Django:ValueError:无法创建表单域,因为它的相关模型尚未加载 [英] Django: ValueError: Cannot create form field because its related model has not been loaded yet
问题描述
工作流程
),我试图为该应用程序制作第一个表单。我的第一个应用程序称为 po
。在工作流程
应用程序中,我有一个名为 WorkflowObject
的类,(现在)只有一个属性 - PurchaseOrder
的外键,它在 po / models.py
中定义。我从po.models import PurchaseOrder 导入了的类。
我想要做的是有一个用户创建一个新的 PurchaseOrder
的页面。这工作正常(这与我在 PurchaseOrder
应用程序中使用的形式相同),然后使用该类的实例来创建一个 WorkflowObject
。现在的问题是,我收到错误: ValueError:不能为'购买'创建表单字段,因为它的相关模型PurchaseOrder尚未加载
。我真的不知道从哪里开始。它工作正常(允许我创建一个新的 PurchaseOrder
并转发到一个url与其主键在url),直到我添加了应该允许我创建一个新的 WorkflowObject
。我会在这里给出具体的看法:
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render,get_object_or_404
从django_tables2导入RequestConfig
从po.models import PurchaseOrderForm,PurchaseOrder
from workflow.models import POObject,WorkflowForm
def new2请求,号码):
po = PurcchaseOrder.objects.get(pk = number)
如果request.method =='POST':
form = WorkflowForm(request.POST)
如果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})
导致错误(或至少是其中的一行如下所示:
class WorkflowForm(ModelForm):
purchase = forms.ModelChoiceField(queryset = PurchaseOrder .objects.all())
编辑:
我似乎做了一个非常noob错误,并在我的 WorkflowObject
的定义中包含括号,也就是说我已经说过 purchase = models.ForeignKey('PurchaseOrder')
,而不是 purchase = models.ForeignKey(PurchaseOrder)
解决方案 p>首先,你可以尝试减少代码:
def new2(request,number):
po = PurcchaseOrder.objects.get(pk = number)
form = WorkflowForm(request.POST或None)
如果form.is_valid():
new_flow = form.save()
return HttpResponse('Good')
else:
return render(request,'new-workflow.html',{'form':form,'purchase' :po})
其次,我不明白为什么你在其他情况下写了 forms.ModelChoiceField(...)
另一种情况 ModelForm
instance forms.ModelForm
?
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)
解决方案 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
?
这篇关于Django:ValueError:无法创建表单域,因为它的相关模型尚未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!