Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form subclassing - How to 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.

like image 479
tamakisquare Avatar asked Dec 22 '22 11:12

tamakisquare


1 Answers

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})
like image 142
DrTyrsa Avatar answered Dec 28 '22 07:12

DrTyrsa