Django Custom User Model
“How do I replace the username field with the email field or add other fields while for registering a new user? ”
Did this question came to your mind before creating any application with django? If yes, then this post is for you.
In this post, I will show you guys how to create custom user model in django.
As we all know the default User model in django uses a username to uniquely identify a user during authentication. If you need to replace this username field with other field lets say an email field then you need to create a custom user model by either subclassing AbstractUser or AbstractBaseUser.
So what are these ?
AbstractUser :- if you only want to remove the username field and don’t have any problem with other fields
AbstractBaseUser :- if you want to start all from scratch by creating your complete new user model
Code
I am assuming you already have created a django-project. So create a new app users.
python manage.py startapp users
Lets create a users/managers.py file
Now create a users/models.py file
Here we firstly created our custom manager by subclassing BaseUserManager, that uses email for unique identifies rather than username
Also for the models.py file, we firstly subclass AbstractUser. Here, we specify email as USERNAME_FIELD. Along wih these we also added a new field to store the Full Name of the user.
Lets create the users/forms.py file
Here in forms.py we have created a basic validation method so that while creating the user, both the passwords are same
users/views.py
It imports our custom user model and saves the fields to the model
Before running the code, lets change our project settings.py file
Here, in settings.py file we firstly added our newly created app. Then we define a AUTH_USER_MODEL as we have created our custom model.
Lets migrate our model
python manage.py makemigration
python manage.py migrate
python manage.py runserver
You can check the urls and other template files from my github repo
Output
In django-admin
Conclusion
In this post, we looked at how to create a custom django user model. You can find the final code here.