How to create your own custom user model in Django?

This article will help you create your own custom user model in Django


Creating your own custom user model is as essential as anything. In the future, if you want to make some changes to the model then it’ll be easy for you to make those changes. If not you have to make some changes to the model and some part of the code is also to be changed. In some sites using email for logging in make some sense when compared to username. So having a custom user model is a good practice for a programmer.

This article will help you create your own custom user model as per your needs. Please follow up on the below steps.

  1. Django setup
  2. Creating a Django app
  3. Custom model for the created app
  4. Migrate the model

After setting up Django enter into your created environment. Then you can create a Django project by the below command.

django-admin startproject mysite

This might create your Django project with all the essential files. Ensure that you are executing the above code in the environment.

If you have noticed some website uses URLs like /accounts/ which looks professional code. So we also need to create an app named accounts where we’ll manage all the things related to the accounts. So create a Django app with the below command in the current environment.

python startapp accounts

Once created the app will contain essential files. The files created are shown below.


Once create all the database model code should be written in the file. Also, don’t forget to create a file that helps for routing links through the accounts app. Once created let’s move to the next step that is building a custom user model as per our needs.

So for creating a custom user model we need to write down the below code in the file because python uses objects to manage and access the data stored in the database. The code for the file is mentioned below.

User class contains the fields or structure of the table where we get some details about the user with some conditions on it. For managing the User class We have UserManager which will be used for creating a new user or a superuser. Here as I said earlier username field will be replaced by email. It’ll make more sense than the username field. The required fields are also mentioned. By using email as a username field it automatically adds the email field as an essential one.

So by the UserManager, we can create a new normal user and a superuser. user.set_password() will hash the plain text password to the default hashing algorithm. Django uses PBKDF2 with SHA-256 hash with a password stretching mechanism. function will save the created user with the field and the hashed password. It is simple to create a custom user model.

One thing to keep in mind is, Don’t migrate the model before creating the model. Once the model has been completed. Before all this, after creating the model we have to update the Django settings file with the custom models else Django will use its own pre-implemented model. Just use this code in the file which will be located in the project directory.

AUTH_USER_MODEL = ‘accounts.User’

Once the model has been set now it’s time to migrate the model to the database.

Now we’re ready to migrate our custom user model by migrating the changes into the database. Use the below command to migrate and execute that code in the current environment.

python makemigrations
python migrate

This command will look for all the models and changes in the models in the Django project. If you want to specifically make migrations for an app you can do that with the below command.

python makemigrations <app_name>
python migrate <app_name>

You can replace <app_name> it with accounts to make changes only on that app alone. If you have any queries comment out. Let me help you!

I’m just planning to write a series for creating a Django API with flutter app as a front end. Please comment it out so I can write it based on your interest. It may include account creation, authentication, and management.



Machine learning enthusiast, App developer, web app developer.

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

Machine learning enthusiast, App developer, web app developer.