Created
September 3, 2018 07:19
-
-
Save gustavi/e93ba3769749a7c596b4bb2043eec8da to your computer and use it in GitHub Desktop.
Django - Use email as "username" field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from django.contrib.auth.admin import UserAdmin | |
from django.utils.translation import gettext_lazy as _ | |
from yourprojectname.member.models import User | |
@admin.register(User) | |
class EmailUserAdmin(UserAdmin): | |
""" | |
Administration for custom User model. | |
""" | |
fieldsets = ( | |
(None, {'fields': ('email', 'password')}), | |
(_('Personal info'), {'fields': ('first_name', 'last_name')}), | |
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', | |
'groups', 'user_permissions')}), | |
(_('Important dates'), {'fields': ('last_login', 'date_joined')}), | |
) | |
add_fieldsets = ( | |
(None, { | |
'classes': ('wide',), | |
'fields': ('email', 'password1', 'password2'), | |
}), | |
) | |
list_display = ('email', 'first_name', 'last_name', 'is_staff') | |
search_fields = ('email', 'first_name', 'last_name',) | |
ordering = ('email',) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import AbstractUser, UserManager | |
from django.db import models | |
from django.utils.translation import gettext_lazy as _ | |
class EmailUserManager(UserManager): | |
""" | |
Custom manager where username field is removed. | |
""" | |
def _create_user(self, email, password, **extra_fields): | |
""" | |
Create and save a user with the given username, email, and password. | |
""" | |
email = self.normalize_email(email) | |
user = self.model(email=email, **extra_fields) | |
user.set_password(password) | |
user.save(using=self._db) | |
return user | |
def create_user(self, email=None, password=None, **extra_fields): | |
extra_fields.setdefault('is_staff', False) | |
extra_fields.setdefault('is_superuser', False) | |
return self._create_user(email, password, **extra_fields) | |
def create_superuser(self, email, password, **extra_fields): | |
extra_fields.setdefault('is_staff', True) | |
extra_fields.setdefault('is_superuser', True) | |
if extra_fields.get('is_staff') is not True: | |
raise ValueError('Superuser must have is_staff=True.') | |
if extra_fields.get('is_superuser') is not True: | |
raise ValueError('Superuser must have is_superuser=True.') | |
return self._create_user(email, password, **extra_fields) | |
class User(AbstractUser): | |
""" | |
Custom User model with "email" as "username". | |
""" | |
email = models.EmailField(_('email address'), unique=True) | |
USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = list() | |
objects = EmailUserManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Note: replace "member" by your app name | |
INSTALLED_APPS = [ | |
# ... | |
'django.contrib.auth', | |
# ... | |
'yourprojectname.member', | |
# ... | |
] | |
AUTH_USER_MODEL = 'member.User' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment