Если вы переопределите форму для пользователей Django, вы можете сделать это довольно изящно.
class CustomUserCreationForm(UserCreationForm):
"""
The form that handles our custom user creation
Currently this is only used by the admin, but it
имеет смысл разрешить пользователям регистрироваться самостоятельно позже "" "email = forms.EmailField (обязательный = True) first_name = forms.CharField (обязательный = True) last_name = forms.CharField (обязательный = True)
class Meta:
model = User
fields = ('first_name','last_name','email')
, а затем в свой backends.py вы можете поместить
class EmailAsUsernameBackend(ModelBackend):
"""
Try to log the user in treating given username as email.
We do not want superusers here as well
"""
def authenticate(self, username, password):
try:
user = User.objects.get(email=username)
if user.check_password(password):
if user.is_superuser():
pass
else: return user
except User.DoesNotExist: return None
тогда в вашем admin.py вы можете переопределить с помощью
class UserCreationForm(CustomUserCreationForm):
"""
This overrides django's requirements on creating a user
We only need email, first_name, last_name
We're going to email the password
"""
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 since we're going to send a reset email to start their account
self.fields['username'].required = False
self.fields['password1'].required = False
self.fields['password2'].required = False
Mine имеет несколько других модификаций, но это должно привести вас на правильный путь.