Custom User Model | Explore Django
3 min read
5 months ago
Published on Jul 16, 2024
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
How to Create a Custom User Model in Django
-
Create a New Django Project:
- Open your terminal and create a new Django project by running:
django-admin startproject myproject
- Open your terminal and create a new Django project by running:
-
Create a New App for the User:
- Inside the project directory, create a new app for the user by running:
python manage.py startapp users
- Inside the project directory, create a new app for the user by running:
-
Update Project Settings:
- Open the
settings.py
file in your project folder and add'users'
to theINSTALLED_APPS
list. - Set the custom user model by adding
AUTH_USER_MODEL = 'users.User'
.
- Open the
-
Create Custom User Manager:
- In the
users/models.py
file, create a custom user manager by importingUserManager
from Django. - Define a function to create users with email, password, and extra fields ensuring a valid email address is provided.
- In the
-
Set Default User Attributes:
- Define default attributes for regular users such as
is_staff
andis_superuser
asFalse
. - Implement a function to create superusers with elevated permissions.
- Define default attributes for regular users such as
-
Define Custom User Model:
- Create a custom user model by inheriting from
AbstractUser
andPermissionsMixin
. - Define fields for email, name, active status, staff status, superuser status, date joined, and last login.
- Create a custom user model by inheriting from
-
Customize User Model Manager:
- Set the custom user manager for the custom user model.
-
Specify User Model Fields:
- Define the
USERNAME_FIELD
as email and theEMAIL_FIELD
as email. - Specify the required fields as an empty list.
- Define the
-
Update Model Meta Information:
- Set verbose names for the user model and plural form as per your preference.
-
Add Custom Methods:
- Implement the
get_full_name
andget_short_name
methods to retrieve the user's name.
- Implement the
-
Migrate Changes to the Database:
- Run the following commands to apply the model changes to the database:
python manage.py makemigrations python manage.py migrate
- Run the following commands to apply the model changes to the database:
-
Create a Superuser:
- Create a superuser by running:
python manage.py createsuperuser
- Follow the prompts to set the email and password for the superuser.
- Create a superuser by running:
-
Access Admin Interface:
- Start the Django development server and navigate to the admin interface in a web browser.
- Log in using the email and password of the superuser created.
-
Verify Custom User Model:
- Register the custom user model in the admin interface to view and manage user details.
- Verify the hashed password, permissions, email, and name associated with the user.
-
Test Custom User Model Functionality:
- Check the
get_short_name
method to see how the user's name is displayed based on the email. - Modify the user's name to see changes in the displayed name.
- Check the
-
Completion:
- Congratulations! You have successfully created a custom user model in Django. This customization provides more flexibility and functionality for user management in your Django project.
By following these steps, you can effectively implement a custom user model in Django for enhanced user authentication and management capabilities.