custom user class code example
Example 1: import abstractuser
from django.contrib.auth.models import AbstractUser
Example 2: custom user
class CustomUser(AbstractBaseUser):
ADMIN = 'admin'
STAFF = 'staff'
STATUS = [
(ADMIN, _('Admin User')),
(STAFF, _('Staff User')),
]
email = models.EmailField(_('email address'), unique=True)
first_name = models.CharField(_('first name'), max_length=30)
last_name = models.CharField(_('last name'), max_length=30)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False) # a admin user; non super-user
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
objects = CustomUserManager()
@staticmethod
def has_perm(perm, obj=None):
# "Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
@staticmethod
def has_module_perms(app_label):
# "Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
def __str__(self):
return "{}".format(self.email)