Last active
August 11, 2020 13:59
-
-
Save olivx/82fa4b5d582d2a977fb9fc0637bb082d to your computer and use it in GitHub Desktop.
Custom User Django
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.db.models import (Model, EmailField, BooleanField, DateTimeField) | |
from django.contrib.auth.models import AbstractBaseUser | |
from django.contrib.auth.models import BaseUserManager, PermissionsMixin | |
class UserManager(BaseUserManager): | |
use_in_migrations = True | |
def _create_user(self, email, password, **extra_fields): | |
user = self.model(email=email, **extra_fields) | |
user.set_password(password) | |
user.save() | |
return user | |
def create_user(self, email=None, password=None, **extra_fields): | |
return self._create_user(email, password, **extra_fields) | |
def create_superuser(self, email=None, password=None, **extra_fields): | |
return self._create_user(email, | |
password, | |
is_admin=True, | |
is_staff=True, | |
is_superuser=True, | |
**extra_fields) | |
class User(AbstractBaseUser, PermissionsMixin): | |
email = EmailField(unique=True, verbose_name="Email") | |
is_admin = BooleanField(default=False) | |
is_active = BooleanField(default=True) | |
is_staff = BooleanField(default=False) | |
is_superuser = BooleanField(default=False) | |
date_joined = DateTimeField(auto_now_add=True) | |
USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = [] | |
objects = UserManager() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment