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,因此Metawith类exclude不起作用。
ModelForm
Meta
exclude
您可以通过覆盖 init 方法来更改子类中的字段:
class LoginFormWithoutNickname(LoginForm): def __init__(self, *args, **kwargs): super(LoginFormWithoutNickname, self).__init__(*args, **kwargs) self.fields.pop('nickname')