Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding more validation to django admin user add forms

If we need to add more validation in django admin user add form like making the first name and last name and email compulsory.. Whats the way of achieving this??

like image 770
Wentworth Avatar asked Jul 18 '11 08:07

Wentworth


1 Answers

You must create your own user form and adding your required fields:

class UserForm(forms.ModelForm):
    class Meta:
        model = User

    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.fields['email'].required = True
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True

Then override the form in your ModelAdmin:

class UserAdmin(admin.ModelAdmin):
    form = UserForm

And then unregister the normal admin User before registering your own:

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
like image 179
Geoffroy CALA Avatar answered Dec 16 '22 21:12

Geoffroy CALA