Quick Code
Published in

Quick Code

Custom User Model in Django 3

Photo by Kevin Ku from Pexels

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.

If you have any suggestions then please let me know.

Give this a clap if it was helpful.

Check out my portfolio: https://theshubhagrwl.netlify.app/

Connect with me on Social Platforms:

LinkedIn: https://www.linkedin.com/in/theshubhagrwl/

Twitter: https://twitter.com/theshubhagrwl/

GitHub: https://github.com/theshubhagrwl

Instagram: https://www.instagram.com/theshubhagrwl/

--

--

--

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Recommended from Medium

Daily Code 4: Golang and remote procedure calls

Antiforgery Tokens Behind The Scenes

What’s new in terraform 0.15

Test Driving a Kata (in Swift!)

Git Repositories

Share (whisky) data with the help of Azure — part 2

Displaying Database table data on Html table tag part 2

CS373 Fall 2021: Ian Winson

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shubh Agrawal

Shubh Agrawal

Just sharing my experience. http://theshubhagrwl.netlify.app/

More from Medium

Introduction to Django?

Django: Files and folder Structure of New Project

Build an OCR App with Django

Django first steps for the total beginners: a quick tutorial