How can I add additional required fields to Django's "User Add" administration page?
When adding a new user to my Django application, I'开发者_JAVA百科d like to ensure that the administration page requires an administrator to include the "email" field.
I've tried tinkering with the UserAdminForm object, but I've come up empty thus far. Any tips?
You'll want to add something like this to one of your project's admin.py files:
from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm from django.contrib.auth.models import Group, User # Override username field require email address class UserCreationForm2(UserCreationForm): email = forms.CharField(max_length=75, required=True) class UserChangeForm2(UserChangeForm): email = forms.CharField(max_length=75, required=True) class UserAdmin2(UserAdmin): form = UserChangeForm2 add_form = UserCreationForm2 admin.site.unregister(User) admin.site.register(User, UserAdmin2)
Essentially, make the email field required, unregister the built-in admin, and register a new admin with the override.
精彩评论