class LoginForm(forms.Form): nickname = forms.CharField(max_length=100) username = forms.CharField(max_length=100) password = forms.CharField(widget=forms.PasswordInput) class LoginFormWithoutNickname(LoginForm): # i don't want the field nickname here nickname = None #??
有没有办法做到这一点?
注意:我没有ModelForm,因此的Meta课程exclude不起作用。
ModelForm
Meta
exclude
你可以通过覆盖init方法来更改子类中的字段:
class LoginFormWithoutNickname(LoginForm): def __init__(self, *args, **kwargs): super(LoginFormWithoutNickname, self).__init__(*args, **kwargs) self.fields.pop('nickname')