How to create your own custom user model in Django?

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

credits: https://www.procoding.org/

What is the need for a custom user model?

Brief

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

1. Django setup

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.

2. Creating Django app

python manage.py startapp accounts

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

accounts/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py

Once create all the database model code should be written in the models.py file. Also, don’t forget to create a urls.py 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.

3. Custom model for the app

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. user.save() 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 settings.py 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.

4. Migrating the model

python manage.py makemigrations
python manage.py 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 manage.py makemigrations <app_name>
python manage.py 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