从__init__初始化django字段

时间:2012-07-30 08:32:11

标签: django django-forms

我有一张表格:

class MatchForm(forms.Form):
    base_field = forms.CharField(widget=forms.Select)

   def __init__(self ,*args, **kwargs):
     BASE_FIELDS = (
                     ('job','JOB'),
                     ('car','CAR'),
     )
     super(MatchForm,self).__init__(*args, **kwargs)
     self.fields['base_field'].choices = BASE_FIELDS

使用我编写的这个类,select中没有任何选项。如何插入BASE_FIELDSbase_field字段中。

提前致谢

1 个答案:

答案 0 :(得分:3)

BASE_FIELDS = (
    ('job','JOB'),
    ('car','CAR'),
)

class MatchForm(forms.Form):
    base_field = forms.ChoiceField(choices=BASE_FIELDS)