Django,创建后,将ChoiceField设置为形式 [英] Django, set ChoiceField in form, after creation
问题描述
我想在其中显示一些自定义用户数据的表单。更具体地说,我想为每个用户填写不同数据的 forms.ChoiceField
。
这是我的 form
:
class WallPostForm(forms.Form):
/ pre>
text = forms.CharField(label = u'',widget = TinyMCE(attrs = {'cols':70,'rows':5}))
relationships_to = forms.ChoiceField(label = u'Relates to' choice = [],widget = forms.Select(),required = False)
def __init __(self,data):
self.fields ['relationships_to'] = forms.ChoiceField label = u'Relates to',choices = data,widget = forms.Select(),required = False)
super(WallPostForm,self).__ init __()
这是我打电话的方式:
user = get_object_or_404(User,username = username)
data = UserTopics.objects.filter(user = user,result = 0).values('id','topic__name')[:10]
form = WallPostForm(data)
我得到一个
'WallPostForm对象没有属性'fields'
错误。
我做错了什么?
解决方案Django在
__ init __
中设置表单的字段
/ p>
所以只需交换代码:
def __init __(self,data )
super(WallPostForm,self).__ init __()
self.fields ['relationships_to'] = forms.ChoiceField(label = u'Relates to',choices = data,widget = forms.Select (),required = False)
尽管您可能不应该覆盖
表单
的__ init __
这样。 Django的表单系统期望init中的数据
arg包含表单的数据,而不是您用于选择字段的查询集。
我会用不同的方式覆盖:
def __init __(self,* args,** kwargs) :
ABOUT_to_queryset = kwargs.pop('RELATED_to_queryset')
super(WallPostForm,self).__ init __(* args,** kwargs)
self.fields ['relationships_to'] =表单。 ChoiceField(label = u'Relates to',choices = relations_to_queryset,widget = forms.Select(),required = False)
然后调用它:
form = WallPostForm(request.POST或None,correl_to_queryset = data)
I want to display a form with some customized user data in it. More specifically I want to fill a
forms.ChoiceField
with different data for each user.This is my
Form
:class WallPostForm(forms.Form): text = forms.CharField(label=u'', widget=TinyMCE(attrs={'cols': 70, 'rows': 5})) relates_to = forms.ChoiceField(label=u'Relates to', choices=[], widget=forms.Select(), required=False) def __init__(self, data): self.fields['relates_to'] = forms.ChoiceField(label=u'Relates to', choices=data, widget=forms.Select(), required=False) super(WallPostForm, self).__init__()
And this is how I am calling this:
user = get_object_or_404(User, username=username) data = UserTopics.objects.filter(user=user, result=0).values('id', 'topic__name')[:10] form = WallPostForm(data)
I get a
'WallPostForm' object has no attribute 'fields'
error.What am I doing wrong?
解决方案Django sets up the form's
fields
property in the__init__
.So just swap your code:
def __init__(self, data): super(WallPostForm, self).__init__() self.fields['relates_to'] = forms.ChoiceField(label=u'Relates to', choices=data, widget=forms.Select(), required=False)
Though, you probably shouldn't override a
Form
's__init__
like that. Django's form system expects thedata
arg in init to contain the data for the form, not a queryset you're using for a choices field.I'd override it differently:
def __init__(self, *args, **kwargs): relates_to_queryset = kwargs.pop('relates_to_queryset') super(WallPostForm, self).__init__(*args, **kwargs) self.fields['relates_to'] = forms.ChoiceField(label=u'Relates to', choices=relates_to_queryset, widget=forms.Select(), required=False)
Then call it:
form = WallPostForm(request.POST or None, relates_to_queryset=data)
这篇关于Django,创建后,将ChoiceField设置为形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!