删除超类变量



我有一个需要继承的类

class AuthenticationForm(forms.Form):
    username = forms.CharField(label=_("Username"), max_length=30)
    password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
    def __init__(self, request=None, *args, **kwargs):
        super(AuthenticationForm, self).__init__(*args, **kwargs)

你能告诉我如何继承这个并从超类中删除username变量吗?

class LoginForm(AuthenticationForm):
    email = forms.EmailField(
        required=True, label=_("Email")
    )
    def __init__(self, request, *args, **kwargs):
        #del super(LoginForm, self).username
        super(LoginForm, self).__init__(
            request, *args, **kwargs
        )

谢谢

因为这是Django,你可以直接从fields字典中删除它:

class LoginForm(…):
    def __init__(…):
        super(LoginForm, self).__init__(…)
        self.fields.pop('username')

最新更新