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:

JavaScript

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:

JavaScript

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:

JavaScript

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

Advertisement