I'm writing a simple app, and I'm totally happy with Django User model, except that I want a user email to be unique and obligatory field. I have developed a solution, but I'm wondering if I'm missing something.
a) If you think that something is missing in the solution below please let me know
b) If you think that you have a better solution please share
This is what I did
- Created a custom user model
# customers/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
email = models.EmailField(verbose_name='Email', unique=True, blank=False, null=False)
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['email']
Added AUTH_USER_MODEL = 'customers.User' to settings.py
Changed a reference to User model:
User = get_user_model()
- Modified admin.py in order to be able to add users using Django admin.
# customers/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth import get_user_model
User = get_user_model()
class CustomUserAdmin(UserAdmin):
list_display = ('username', 'email', 'is_staff')
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2'),
}),
)
admin.site.register(User, CustomUserAdmin)