By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

How to create Custom User Model or Extend User Model in Django?

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 your own filed as username field. Just keep the following line in your User model to make user authenticate with your own field rather than Django's default username field. To use your own model field as USERNAME_FIELD make sure it is unique, or it will raise 'User.your_field' must be unique because it is named as the 'USERNAME_FIELD'.

class User(AbstractUser):
    myname = models.CharField(max_length=100, unique=True)

    USERNAME_FIELD = 'myname'

 

To Know more about our Django CRM(Customer Relationship Management) Open Source Package. Check Code

    Posted On
  • 21 December 2015
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Custom Password Less Authentication in Django

Authentication backends allow the ability to change what method checks your users credentials.

For web services, ie Facebook authentication, you don't have access to user ...

Continue Reading...
Improving page speed score in Google Page Score test- PART1

Google's Page Speed Insights measures the performance of a page for mobile devices and desktop devices. It fetches the url twice, once with a mobile ...

Continue Reading...
Django Model managers and properties

Django model is the single, definitive source of data about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties