Custom User Model in Django 3
Tags: python, django, webdev, codenewbie
Published: 2020-08-16 06:01:06

<link rel="canonical" href="https://medium.com/@theshubhagrwl/custom-user-model-in-django-3-3750fb5656c4"/>

# 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 <i>createsuperuser</i> 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. 

{% github theshubhagrwl/django_custom_user_app no-readme %}

I have made a [GitHub](https://github.com/theshubhagrwl/django_custom_user_app) repo, so if you want you can directly use that (instructions are there)

# Let's Get Started
First of all, make a <br>Django Project</b> and create an app called <b>users</b>

## Now we can start editing the models.py file inside our users app
Before editing let's do some theory. <br>
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
{% gist https://gist.github.com/theshubhagrwl/1b8dd0987c84e820e9fd7befa0d7aecc %}

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. <br>

## 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. <br>
After the manager, we have our usual model.<br> 
We have set ***username*** to None because we do not want to include a username.<br>
***USERNAME_FIELD*** in it indicated which we declare 'email'. This should be unique.<br>
*session_token* is an optional field. I have it there because I was making my custom token. <br>
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
```python 
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.
```shell
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

```python
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*
<br>
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/

This post was originally written on [Medium](https://medium.com/@theshubhagrwl/custom-user-model-in-django-3-3750fb5656c4?source=friends_link&sk=003e40175d9f84f9eb6d5ba0c0c7e70d)