How to create Custom User Model in Django?

Reading Time : ~ .

Django provides built-in authentication which is good for most of the cases, but you may have needs that are being served by the existing system. For Ex: You want 'email' for authentication purpose rather than Django's username field and you want an extra field called 'display_name' as a full name for the logged in User. To meet the above requirements, we need to customize Django's built-in user model or substitute a completely customized model. In this blog post, we'll learn how to customize Django's built-in User model. Its good idea to extend Django User Model rather than writing whole new user model and there are a lot of ways but one good way is to extend it from Django itself. We get all features and properties of Django User and our own custom features on top of it.

Add the following to your app that holds custom user model.

In models.py 

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    name = models.CharField(max_length=100, blank=True, null=True)

and specify your custom user model in settings.py

AUTH_USER_MODEL = ‘your_app.User'


If you want to use your custom user model in models.py, you can get that model with `settings.AUTH_USER_MODEL` or `get_user_model()` of Django's auth module.

   from django.conf import settings
   from django.contrib.auth import get_user_model

   class Book(models.Model):
        author = models.ForeignKey(settings.AUTH_USER_MODEL)

(OR)

   from django.contrib.auth import get_user_model

   User = get_user_model()

   class Book(models.Model):
        author = models.ForeignKey(User)

By overriding the Django's Abstract user model we can specify our own username field by overriding 'USERNAME_FIELD', By default Django User model uses 'username' field for authentication purpose, we can set 'email' as username field. Just keep the following line in your User model to make user authenticate with email rather than Django's default username field.

class User(AbstractUser):
    name = models.CharField(max_length=100, blank=True, null=True)

    USERNAME_FIELD = 'email'

 

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Vim for Python Web Development Ashwin Kumar

Having a good environment setup is important for effective, fast and easy coding. We have different IDE's like eclipse, pycharm, sublime etc.. which are powerful ...

Continue Reading...
Google Analytics Graphs to your Dashboard in Python Web Frameworks Jagadeesh V

Ecommerce solution providers like OpenCart, Magento Provide extensions to see Google analytics data in their own dashboards as graphs. whereas there are no such plugins ...

Continue Reading...
Basics of Linux File System Heirarchy Dinesh Deshmukh

In 1994, FHS(File System Hierarchy Standard) was proposed, it describes the directory structure of UNIX and UNIX-like systems(Linux). Before FHS was proposed, there had been ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties