Django Form Subclass - How do I change some attribute while preserving the other attributes of the inherited field?

My question is about subclassing forms in Django. How to change some attribute while keeping other attributes of the 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 redefine the widget with widget = TextInput (attrs = {'size: 30}), while retaining the label. Is it possible? If so, how? Thanks.

+4
source share
1 answer

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}) 
+10
source

All Articles