25
loading...
This website collects cookies to deliver better user experience
Email
instead of the standard Username
that comes with the Django default model.models.py
file to include our user profile model.AbstractBaseUser
has the authentication functionality only , it has no actual fields, you will supply the fields to use when you subclass.PermissionsMixin
. This is an abstract model you can include in the class hierarchy for your user model, giving you all the methods and database fields necessary to support Django’s permission model.username
, email
, is_staff
, is_active
, is_superuser
, last_login
, and date_joined
fields the same as Django’s default user, you can install Django’s UserManager
; however, if your user model defines different fields, you’ll need to define a custom manager that extends BaseUserManager
.from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.models import BaseUserManager
UserProfile
and inherit from the AbstractBaseUser
and PermissionsMixin
class UserProfile(AbstractBaseUser, PermissionsMixin):
""" Database model for users in the system """
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255)
objects = UserProfileManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def __str__(self):
""" Return string representation of our user """
return self.email
email
and name
field required and made email
uniqueUSERNAME_FIELD
which defines the unique identifier for the username
to email
.UserProfileManager
UserProfileManager
and inherit from the BaseUserManager
.class UserProfileManager(BaseUserManager):
""" Manager for user profiles """
UserProfileManager
the way it manages work is you specifyUserProfileManager
class.def create_user(self, email, name, password=None):
""" Create a new user profile """
if not email:
raise ValueError('User must have an email address')
email = self.normalize_email(email)
user = self.model(email=email, name=name)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, name, password):
""" Create a new superuser profile """
user = self.create_user(email,name, password)
user.is_superuser = True
user.is_staff = True
user.save(using=self._db)
return user
settings.py
to use this model over default one.AUTH_USER_MODEL = 'App_name.UserProfile'
$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py createsuperuser
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
Username
it is prompting for Email
.