This article will help you create your own custom user model in Django
What is the need for a custom user model?
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.
- Django setup
- Creating a Django app
- Custom model for the created app
- Migrate the model
1. Django setup
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.
2. Creating Django app
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 manage.py 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
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
So for creating a custom user model we need to write down the below code in the
models.py file because python uses objects to manage and access the data stored in the database. The code for the
models.py 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. 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
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 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.