Django - ModelChoiceField - TypeError - __init __()至少需要2个参数(1给出)
问题描述:
我在Django 1.2上。我试图在表单中使用ModelChoiceField。为什么这些失败与上述错误信息?我在:-(Django - ModelChoiceField - TypeError - __init __()至少需要2个参数(1给出)
class QueueForm(forms.Form):
queue = forms.ModelChoiceField(query_set=Order.objects.all())
损失我也试过这样:
class QueueForm(forms.Form):
queue = forms.ModelChoiceField(query_set=Order.objects.all(),required=False)
,并得到:
__init__() takes at least 2 arguments (2 given)
这似乎在说,这是在队列中发生的事情= ..在我使用表格之前
答
你在构造函数中设置了错误的变量名,它需要是queryset而不是t query_set。试试这个:
class QueueForm(forms.Form):
queue = forms.ModelChoiceField(queryset=Order.objects.all())
哇!我知道它必须是这样的: - (我觉得很愚蠢 – Greg 2011-12-30 17:58:14
很高兴帮助!如果Django函数参数具有更一致的变量名称约定,那将是很好的。 – checker 2011-12-30 18:27:57