Created
March 6, 2018 18:25
-
-
Save Aperyon/31a97a3213f201595745ae6a837324f2 to your computer and use it in GitHub Desktop.
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 import models | |
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser | |
class UserManager(BaseUserManager): | |
def create_user(self, email, password=None): | |
if not email: | |
raise ValueError('Users must have an email address') | |
user = self.model(email=self.normalize_email(email)) | |
user.set_password(password) | |
user.save(using=self._db) | |
return user | |
def create_superuser(self, email, password): | |
user = self.create_user(email, password=password) | |
user.is_admin = True | |
user.save(using=self._db) | |
return user | |
class User(AbstractBaseUser): | |
email = models.EmailField(max_length=255, unique=True) | |
is_active = models.BooleanField(default=True) | |
is_admin = models.BooleanField(default=False) | |
objects = UserManager() | |
USERNAME_FIELD = 'email' | |
def __str__(self): | |
return self.email | |
def has_perm(self, perm, obj=None): | |
return True | |
def has_module_perms(self, app_label): | |
return True | |
@property | |
def is_staff(self): | |
return self.is_admin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment