django下ChoiceField等, queryset 动态设定

定义:
class XXXForm(forms.Form):
    def __init__(self, query_set=None, *args, **kwargs):
        super(XXXForm, self).__init__(*args, **kwargs)
        roles = forms.ModelMultipleChoiceField(label=u"角色:",
                                           required=False,
                                           widget=CheckboxSelectMultiple,
                                           queryset=query_set)
        self.fields['roles'] = roles
        
    password = forms.CharField(label="修改密码:",
                               widget=forms.PasswordInput,
                               min_length=6,
                               max_length=32,
                               required=False,
                               error_messages={"required"   : u"请输入密码",
                                                "min_length" : "密码长度为6-32个字符",
                                                "max_length" : "密码长度为6-32个字符"})

使用:
role_form = forms.XXXForm(query_set=query_set, initial=initial_dict)

你可能感兴趣的:(django,query,Class)