Shubh Agrawal
Shubh's Blog

Shubh's Blog

Custom User Model in Django 3

Custom User Model in Django 3

Shubh Agrawal's photo
Shubh Agrawal

Published on Aug 16, 2020

5 min read

Introduction

Hi, in this post we will learn to make a Custom User Model in Django 3 and we will also change the default login functionality of the Django Admin. We will use Email and Password to log in.

Motivation

I had to make a Custom User for my app, I was able to make the model but the problem was createsuperuser command was not working. To debug it I had to do a lot of research and the problem was most of the resources at that time were outdated, so I decided to write this post.

I have made a GitHub repo, so if you want you can directly use that (instructions are there)

Let's Get Started

First of all, make a Django Project and create an app called users

Now we can start editing the models.py file inside our users app

Before editing let's do some theory.
What are managers in Django?

A Manager is an interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application.- Django Docs

Simply speaking Managers provide us a way to manage our model. We can implement this by making our model a subclass of the Manager Class. The manager class is the place where commands like createsuperuser can be edited. Now open up the models.py and put the following code in it

The most important thing to note here is the is_staff and is_superuser attributes. Making a typo in this can cause a hard time debugging.

What have we done here?

We have made a manger for our user model. Inside of this, we have made 2 functions called create_user and create_superuser. create_user, as the name suggests, creates a new user, and create_superuser is used to create a superuser by setting is_staff and is_superuser to true.
After the manager, we have our usual model.
We have set username to None because we do not want to include a username.
USERNAME_FIELD in it indicated which we declare 'email'. This should be unique.
session_token is an optional field. I have it there because I was making my custom token.
The last line of the models.py indicates that the CustomUser is an object of UserManager.

Important Thing

After you have made the model, Open settings.py file and add a line in it

AUTH_USER_MODEL = 'users.CustomUser'

Django allows you to override the default user model by providing a value for the AUTH_USER_MODEL setting that references a custom model. This dotted pair describes the name of the Django app (which must be in your INSTALLED_APPS), and the name of the Django model that you wish to use as your user model. -Django Docs

Last Steps

Now you can run the migrations commands and create superuser also.

py manage.py makemigrations
py manage.py migrate
py manage.py createsuperuser

It will ask for your email and password. Give it the details.

Don't Forget to register the app in admin

admin.site.register(CustomUser)

Now you can run the server and login with your email and password in the admin panel
Enjoy 😎
If you have any suggestions then please let me know
Check out my portfolio: theshubhagrwl.netlify.app

Connect with me on Social Platforms:

LinkedIn: linkedin.com/in/theshubhagrwl

Twitter: twitter.com/theshubhagrwl

GitHub: github.com/theshubhagrwl

Instagram: instagram.com/theshubhagrwl

This post was originally written on Medium

 
Share this