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.
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})
精彩评论