Django: Overriding ModelAdminForm's field attributes
I am trying to override a ModelAdmin's form field as below:
class MyModelAdmin(admin.ModelAdmin):
def ge开发者_运维百科t_form(self, *args, **kwargs):
form = super(MyModelAdmin, self).get_form(*args, **kwargs)
form.fields['password'].help_text = "..."
return form
I am getting an Attribute error: "type object 'FailedLoginForm' has no attribute 'fields'".
Isn't ModelAdmin.get_form() returning a regular Django form object. How come it doesn't have fields?
I suggest that you simply override the form that you want.
class UserCreationForm(UserCreationForm):
# we want to use the email as the username
def __init__(self, *args, **kwargs):
super(UserCreationForm, self).__init__(*args, **kwargs)
# let's require these fields
self.fields['email'].required = True
self.fields['first_name'].required = True
self.fields['last_name'].required = True
# let's not require these
self.fields['username'].required = False
self.fields['password1'].required = False
self.fields['password2'].required = False
From there you can do pretty much anything you want.
精彩评论