Django User Model
Overview
Django User Model is included in the Django Authentication package. It offers a standard model that serves as the foundation for our user accounts. When we begin a new project, the Django user model already provides us with the necessary basic fields. We may need to add more fields in the future. As a result, we modify the Django user model. It is the mechanism for identifying our web application's users. Therefore, it is related to uuser authentication.
Pre-requisites
Before moving on to the creation and extension of the Django user model, you must first meet the following prerequisites:
- The Latest version of Django
- Creation of a virtual environment
- Creation of Django project and application
Introduction to User Model in Django
Django provides a feature of the User model and authentication support. The User model is the fields that Django provides to the user for its authentication. Now, the developer may want some other fields to be used for the authentication of the user. Say he wants to take the email of the user and not the default username. To do the same, developers modify the default Django User model. Therefore, this enables us to store more data and information related to the user.
How to Create a Custom Django User Model?
We initially create a project and then an app after activating a virtual environment in the first two lines. ve venue is the name of my virtual environment. Django-admin start project command is used to start a new project in Django.cd is used to get inside the directory.python manage.py startappis used for creating an application inside the project.
After doing this, we are supposed to register the app in the settings. py file, under the INSTALLED_APPS section.
To test the login, we must first create a test user and a test superuser. Therefore, in the test. py` we write the following code:
In the above code,
- We create a class Test under TestCase.
- We create a function create_userT and create_superuserT.
- In user, we select the get_user_model method and set default values to is_active, is_staff, and is_superuser.
Django manager is a class that serves as an interface between Django models and databases. It is used to retrieve a queryset of objects based on the properties passed as arguments. If no argument is given, it will return all of the objects. We need to define a UserManager class for our custom user model because we will be modifying the initial Queryset that the default Manager class returns. This can be done by extending from BaseUserManager and providing two extra methods create_user and create_superuser. Now we are supposed to create a filemanagers. pyinside the folder of the app. We write the following code
In the above code,
- We created the class UserManager under BaseUserManager.
- create_user() creates and saves a user with the email and password. It will automatically lowercase the email.
- create_superuser() creates and saves a superuser with the email and password.
Now we create the custom user model by making sure that the fields we want for the authentication of the user are set in the the`models. py file.
In the above code,
- We created a class User that inherits the AbstractUser.
- We added fields for user_name and email_id.
- The user_name is set to None so that we can authenticate the user by its unique email and not username.
- The USERNAME_FIELDS defines the field to be used for authentication. We selected email_id to authenticate the user using the same.
- The REQUIRED_FIELD is used to set that the fields which are included in it will be accepted even if left blank.
- We link the manager UserManager in the managers.py file with the variable object.
Our next step is to register a custom user setting. py file. To do the same, we need to add an extra line in the file.
In our case, it is
In the command prompt, we will run the following command
Now we are supposed to create a superuser using the following command:
We will notice that we will be asked to enter the email address instead of the username since in the themodels. py file we chose the email address and set the username to None.
How to Use the User Model in Django?
We can use the User Model in Django in three different ways:
- User
- AUTH_USER_MODEL
- get_user_model()
At first, we are supposed to activate the virtual environment and create a project Book and an app List.
Method 1 - User Model Directly
In this method, we are simply creating a model and registering it. In themodels. py file, under the field author, we mentioned User inside the foreign key.
Our next step is to register the model in the admin. profilee.
Method 2 - AUTH_USER_MODEL
When referring to a user model in a models.py file, the recommended approach is AUTH USER MODEL. In some cases, we need an email to authenticate the user instead of a username. This method enables us to do the same. There are two subclasses to create a custom user model:
- AbstractUser: According to the Django documentation, AbstractUser provides the complete implementation of the default User as an abstract model, which means we will get all of the fields that come with the User model plus any fields that we want to add. This subclass is recommended when we are not willing to change the existing field in the user model but just want to authenticate the user using email or some other parameter and not username.
- AbstractBaseUser: This method is used when we want to create a completely new user model. It only contains authentication functionality and not fields. AbstractBaseUser only provides authentication functionality; it lacks actual fields, we will have to mention the fields and also how the users will be managed.
If our custom user model is already created then, in the settings.py file, we add a new line
In the models.py file, under the field author, we mentioned settings.AUTH_USER_MODEL inside foreign key to use this AUTH_USER_MODEL method.
The next step is to register the model in the admin.p y``file.
Method 3 - get_user_model()
In themodels. py, we import get_user_modelfrom Django.contrib.auth, initialize a variable user with get_user_model(), and mention the variable user in the field author inside the foreign key.
Next, we register the model in the admin.p y``file.
Methods to Extend Django User Model
Specific methods using which we can extend the Django user model are:
- Extend the Django user model with AbstractUser
- Extend the Django user model with AbstractBaseUser
- Extend the Django user model using a proxy model
- Extend the Django User Model Using a One-To-One Link
1. Extend the Django user model with AbstractUser
Using this method, we can add extra fields to our model. In the models. py`,
The next step is to update the settings. py``file by adding an extra line `
In our case, it is
2. Extend the Django user model with AbstractBaseUser With this method, we can create a completely new user model. In the models.py file, we imported the UserManager and used it inside the model Model_User, where we specified the fields we want from the user. We select email under USERNAME_FIELD to use it for authentication.
- We created a class Model_User under AbstractBaseUser.
- We linked UserManager() in the object.
- get_full_name returns the first name along with the last name of the user.
- get_first_name returns the first name of the user.
- email returns the email of the user.
- A class UserManager is created under BaseUserManager.
- create_u_s_e_r creates and saves a User with the given email and password. Our next step is to add an extra line to the the settings.py` file,
In our case, it is
3. Extend the Django user model using a proxy model
In this method, we create an extra proxy model pro and assign it to be a proxy by mentioning 'proxy=true''inside the metaclass.
Since under the class Meta, 'date' is mentioned in the ordering, this means that the result will be displayed based on the date.
4. Extend the Django User Model Using a One-To-One Link We use a one-to-one link to extend the user model when we need to collect extra information about an existing User model that isn't related to the authentication process. To extend the Django user model using a one-to-one link we first import User and then under the model Pro we specify a one-to-one field with User in the field user. Then we create extra fields which we wish to take from the user. In the models. py`,
In the above code,
- We created a class Pro and linked the user using a one-to-one field.
- We linked the create_profile with save_profile methods to the model. This signal with links the two to the model is calledpost_save.
Conclusion
Hello Developer! I am sure by now you must have learned about the creation and extension of the Django User Model. Let us summarize what we got to know so far
- Django User Model provides a standard model that serves as the foundation for your user accounts.
- Django provides a feature of the User model and authentication support.
- It is the mechanism for identifying our web application's users.
- Django enables the developer to customize the User model according to the requirement of the project.
- We can use the User Model in Django in three different ways User, AUTH_USER_MODEL, and get_user_model().
- Methods using which we can extend the Django user model include using AbstractUser, using AbstractBaseUser, using a proxy model, or using a one-to-one link.