Facebook

How to use custom UserModel in django

In previous article we learned how to use django authentication in your application. But sometimes it may happen that default user model of django does not fit your requirement. You may want to add more fields to it. Now there are multiple ways to do it.

1. Use proxy model. No new model is created.
2. Use one-to-one relationship and create another model.
3. Create your own user model and use default authentication system.
4. Create your own user model and create your own authentication system.

Previous article - http://www.newbie42.com/2016/09/implementing-authentication-in-django.html

We will be learning 3rd method here.

Note : This needs to be done before performing any migration.

- First create the custom user model and custom user manager.

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.utils import timezone
from django.db.models import Max

class MyUserManager(BaseUserManager):
use_in_migrations = True
def create_user(self,login, parent_type, last_name, first_name, password):
return create_superuser(self,login, parent_type, last_name, first_name, password)

def create_superuser(self,login, parent_type, last_name, first_name, password):
maxx = self.model.objects.all().aggregate(Max('sys_id'))
if maxx["sys_id__max"] is None:
maxx["sys_id__max"] = 0
user = self.model(
                 sys_id = maxx["sys_id__max"] + 1,
                 login = login,
                 parent_type = parent_type,
                 last_name = last_name,
                 first_name = first_name,
                 display_name = last_name + " " + first_name,
                 created_when = timezone.now()
                 )
user.set_password(password)
user.save(using=self._db)
# no difference here...actually can set is_admin = True or something like that.
return user

class UserModel(AbstractBaseUser):
# custom user class

sys_id = models.BigIntegerField(primary_key=True, blank=True)
last_name = models.CharField(null=False, blank=False, max_length=40)
first_name = models.CharField(max_length=40, null=False, blank=False)
display_name = models.CharField(max_length=80, unique=True, null=False, blank=True)
login = models.CharField(max_length=40, unique=True, null=False, blank=False)
authentication_method = models.CharField(max_length=80, null=True, blank=True)
access_valid_start = models.DateTimeField(null=True, blank=True)
access_valid_end = models.DateTimeField(null=True, blank=True)
notes = models.CharField(max_length=2048, null=True, blank=True)
is_active = models.BooleanField(default=True)

objects = MyUserManager()

USERNAME_FIELD = "login"
# REQUIRED_FIELDS must contain all required fields on your User model, 
# but should not contain the USERNAME_FIELD or password as these fields will always be prompted for.
REQUIRED_FIELDS = [ 'last_name', 'first_name']

class Meta:
app_label = "appname"
db_table = "Users"

def __str__(self):
return self.display_name

def get_full_name(self):
return self.display_name

def get_short_name(self):
return self.last_name


- Now in your settings.py file add this line. It will tell your application that you have to use your custom model for authentication instead of default one.

AUTH_USER_MODEL = 'accounts.UserModel'

- Write your login/logout functions just like in previous article.
-

No comments:

Post a Comment