本文介绍了动态更新ModelForm中ModelMultipleChoiceField的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试更新ModelForm中的ModelMultipleChoiceField的查询。BaseWriteForm是一个ModelForm,我的类看起来如下:
类MyWriteForm(BaseWriteForm):
用于验证用户的表单,用于撰写消息。
recipient = forms.ModelMultipleChoiceField(label = _('To'),
widget = forms.SelectMultiple(attrs = {'class':'chzn-select'}),
queryset = User.objects.all())
def __init __(self,users_list,** kw):
self.fields ['recipient']。queryset = User.objects .filter(pk__in = users_list)
super(BaseWriteForm,self).__ init __(** kw)
class Meta(BaseWriteForm.Meta):
fields =('recipient','subject','body')
在这种情况下我有:'MyWriteForm'ob ject没有属性'fields'
谢谢!
解决方案
在 super()
call()之后移动它。
def __init__ (self,users_list,** kw):
super(BaseWriteForm,self).__ init __(** kw)
self.fields ['recipient']。queryset = User.objects.filter(pk__in = users_list)
I am trying to update the queryset of a ModelMultipleChoiceField in a ModelForm.
BaseWriteForm being a ModelForm, my class looks like as follows:
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')
In this case I am having: 'MyWriteForm' object has no attribute 'fields'
Thanks !
解决方案
Just move it after the super()
call().
def __init__(self, users_list, **kw):
super(BaseWriteForm, self).__init__(**kw)
self.fields['recipients'].queryset = User.objects.filter(pk__in=users_list)
这篇关于动态更新ModelForm中ModelMultipleChoiceField的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!