我使用的是Django 1.2。我正在尝试在表单中使用ModelChoiceField。为什么这些失败并显示上述错误消息?我不知所措:-(

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而不是query_set。尝试这个:

class QueueForm(forms.Form):
queue = forms.ModelChoiceField(queryset=Order.objects.all())

关于python - Django-ModelChoiceField-TypeError-__init __()至少接受2个参数(给定1个),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8681298/

10-11 07:27