如何解决Django形式的queryset要求

时间:2018-09-13 01:15:15

标签: python django

我正在工作Django 2.0网站,并尝试构建Django表单:

forms.py:

class NewMemberstatusform(forms.Form):
    rank = forms.ModelChoiceField(queryset=models.Rank.objects.all(), widget=forms.ModelChoiceField(
        attrs={
        'class':'form-control'
        }
    ))
    unit = forms.ModelChoiceField(queryset=models.Unit.objects.all(), widget=forms.ModelChoiceField(
        attrs={
        'class':'form-control'
        }
    ))
    activate = forms.ChoiceField(choices=YesNo.YESNO_CHOICES, label="Enable Member to see their information", widget=forms.ChoiceField(
        attrs={
        'class':'form-control'
        }
    ))

我的问题是它不能正确编译。

我收到此错误,表示我在等级字段中缺少查询集,但是如您所见,它显然在类中。我想念什么?谢谢

    class NewMemberstatusform(forms.Form):
  File "/Users/sinistersparrow/PycharmProjects/ifthqcom/app_svcrecord/forms.py", line 126, in NewMemberstatusform
    'class':'form-control'
TypeError: __init__() missing 1 required positional argument: 'queryset'

1 个答案:

答案 0 :(得分:-2)

尝试为模型使用import语句:

from <myapp>.models import Rank, Unit

class NewMemberstatusform(forms.Form):
    rank = forms.ModelChoiceField(queryset=Rank.objects.all(), widget=forms.ModelChoiceField(
        attrs={
        'class':'form-control'
        }
    ))
    unit = forms.ModelChoiceField(queryset=Unit.objects.all(), widget=forms.ModelChoiceField(
        attrs={
        'class':'form-control'
        }
    ))
....