开发者

Django form subclassing - How to modify some attribute, while retaining the other attributes, of an inherited field?

开发者 https://www.devze.com 2023-03-20 07:06 出处:网络
My question is regarding form subclassing in Django. How would I modify some attribute, while retaining the other attributes, of an inherited field?

My question is regarding form subclassing in Django. How would I modify some attribute, while retaining the other attributes, of an inherited field?

For example, I have a form, called SignUpForm, which subclasses from UserCreationForm.

开发者_高级运维

UserCreationForm:

...
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
...

In SignUpForm, I would like to override widget with widget=TextInput(attrs={'size': 30}) while keeping label the same. Is this possible? If so, how? Thanks.


You can do it in __init__

def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    self.fields['password1'].widget = TextInput(attrs={'size': 30})
0

精彩评论

暂无评论...
验证码 换一张
取 消