Skip to main content
 首页 » 编程设计

python之Django之ModelChoiceField之TypeError之__init__() 至少需要 2 个参数(给定 1 个)

2025年05月04日37over140

我正在使用 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) 

这似乎是说这发生在 queue = .. 行上。在我使用表格之前。

请您参考如下方法:

您在构造函数中设置了错误的变量名称,它需要是 queryset 而不是 query_set。试试这个:

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