Skip to content
Advertisement

Django: ValueError: Cannot create form field because its related model has not been loaded yet

I’m having some trouble with a Django project I’m working on. I now have two applications, which require a fair bit of overlap. I’ve really only started the second project (called workflow) and I’m trying to make my first form for that application. My first application is called po. In the workflow application I have a class called WorkflowObject, which (for now) has only a single attribute–a foreign key to a PurchaseOrder, which is defined in po/models.py. I have imported that class with from po.models import PurchaseOrder.

What I’m trying to do is have a page where a user creates a new PurchaseOrder. This works fine (it’s the same form that I used in my PurchaseOrder application), and then uses that instance of the class to create a WorkflowObject. The problem now, is that I get the error: ValueError: Cannot create form field for 'purchase' yet, because its related model 'PurchaseOrder' has not been loaded yet. I’m really not sure where to start with this. It was working ok (allowing me to create a new PurchaseOrder and forward to a url with its primary key in the url) until I added the view that should allow me to create a new WorkflowObject. I’ll put that specific view here:

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django_tables2 import RequestConfig
from po.models import PurchaseOrderForm, PurchaseOrder
from workflow.models import POObject, WorkflowForm


def new2(request, number):
 po=PurcchaseOrder.objects.get(pk=number)
 if request.method == 'POST':
   form = WorkflowForm(request.POST)
   if form.is_valid():
      new_flow = form.save()
      return HttpResponse('Good')
   else:
      return render(request, 'new-workflow.html', {'form': form, 'purchase': po})
  else:
    form = WorkflowForm()
    return render(request, 'new-workflow.html', {'form': form, 'purchase': po})

The lines of code that seem to be causing the error (or at least, one of the lines that is shown in the traceback) is:

class WorkflowForm(ModelForm):
   purchase = forms.ModelChoiceField(queryset = PurchaseOrder.objects.all())

EDIT: I seem to have made a very noob mistake, and included parentheses in my definition of WorkflowObject, that is, I had said purchase=models.ForeignKey('PurchaseOrder'), instead of purchase=models.ForeignKey(PurchaseOrder)

Advertisement

Answer

Firstly, you can try reduce code to:

def new2(request, number):
    po=PurcchaseOrder.objects.get(pk=number)

    form = WorkflowForm(request.POST or None)
    if form.is_valid():
        new_flow = form.save()
        return HttpResponse('Good')
    else:
        return render(request, 'new-workflow.html', {'form': form, 'purchase': po})

Secondly, I not understood why you at other case wrote forms.ModelChoiceField(...) and another case ModelForm instance forms.ModelForm ?

Advertisement