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)
Seems, that there are nothing special in your WorkflowForm, so you can define it as follows:
class WorkflowForm(ModelForm):
class Meta:
model = WorkflowObject
Field for relation will be created automatically.
Documentation: Creating forms from models
I had a similar problem and was able to resolve this by declaring all my modelForm classes below all my class models in my models.py file. This way the model classes were loaded before the modelForm classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With