Custom User Model - ( User Management in Django )
Heading h2
Syntax
To create a custom user model in Django, you can create a new model that extends Django's AbstractBaseUser
and BaseUserManager
classes.
# models.py
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class MyUserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError('The Email field must be set')
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_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self.create_user(email, password, **extra_fields)
class MyUser(AbstractBaseUser):
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=30, null=True, blank=True)
last_name = models.CharField(max_length=30, null=True, blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
Example
Here is an example of using this custom user model in Django.
# settings.py
AUTH_USER_MODEL = 'myapp.MyUser'
Output
There will be no output displayed on the console or in the browser. However, the custom user model will allow you to have more control over the authentication and authorization process in your Django web application.
Explanation
Django's default User model is a good starting point for most projects, but if you'd like to customize it, can create your own custom User model.
To create a custom User model, you will need to create a new model that extends Django's AbstractBaseUser
and BaseUserManager
classes. The AbstractBaseUser
class provides the necessary attributes and methods for authentication and authorization, while the BaseManager
class provides the necessary methods for creating and managing users.
Once you have defined your custom User model, you will need to specify it as the AUTH_USER_MODEL in your Django settings file.
Use
Custom User models are useful when you need to store additional information about your users or perform custom authentication and authorization logic.
Important Points
- Custom User models are created by extending Django's
AbstractBaseUser
andBaseUserManager
classes - You will need to specify your custom User model in your Django settings as the AUTH_USER_MODEL
- Custom User models allow you to add additional fields and methods to your User model and perform custom authentication and authorization logic
Summary
In summary, creating a Custom User Model in Django involves creating a new model that extends AbstractBaseUser
and BaseUserManager
, specifying it as the AUTH_USER_MODEL in your Django settings, and then customizing the authentication and authorization process to fit your needs. This allows you to store additional information about your users and perform custom authentication and authorization logic in your Django web application.