我正在尝试在ModelForm中更新ModelMultipleChoiceField的查询集。
BaseWriteForm是一个ModelForm,我的类如下所示:
class MyWriteForm(BaseWriteForm):
"""The form for an authenticated user, to compose a message."""
recipients = forms.ModelMultipleChoiceField(label=_('To'),
widget=forms.SelectMultiple(attrs={'class': 'chzn-select'}),
queryset = User.objects.all())
def __init__(self, users_list, **kw):
self.fields['recipients'].queryset = User.objects.filter(pk__in=users_list)
super(BaseWriteForm, self).__init__(**kw)
class Meta(BaseWriteForm.Meta):
fields = ('recipients', 'subject', 'body')
在这种情况下,我有:' MyWriteForm'对象没有属性'字段'
谢谢!
答案 0 :(得分:9)
只需在super()
调用()之后移动它。
def __init__(self, users_list, **kw):
super(BaseWriteForm, self).__init__(**kw)
self.fields['recipients'].queryset = User.objects.filter(pk__in=users_list)