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

  1. Create a New Django Project:

    • Open your terminal and create a new Django project by running:
      django-admin startproject myproject
      
  2. 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
      
  3. Update Project Settings:

    • Open the settings.py file in your project folder and add 'users' to the INSTALLED_APPS list.
    • Set the custom user model by adding AUTH_USER_MODEL = 'users.User'.
  4. Create Custom User Manager:

    • In the users/models.py file, create a custom user manager by importing UserManager from Django.
    • Define a function to create users with email, password, and extra fields ensuring a valid email address is provided.
  5. Set Default User Attributes:

    • Define default attributes for regular users such as is_staff and is_superuser as False.
    • Implement a function to create superusers with elevated permissions.
  6. Define Custom User Model:

    • Create a custom user model by inheriting from AbstractUser and PermissionsMixin.
    • Define fields for email, name, active status, staff status, superuser status, date joined, and last login.
  7. Customize User Model Manager:

    • Set the custom user manager for the custom user model.
  8. Specify User Model Fields:

    • Define the USERNAME_FIELD as email and the EMAIL_FIELD as email.
    • Specify the required fields as an empty list.
  9. Update Model Meta Information:

    • Set verbose names for the user model and plural form as per your preference.
  10. Add Custom Methods:

    • Implement the get_full_name and get_short_name methods to retrieve the user's name.
  11. 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
      
  12. Create a Superuser:

    • Create a superuser by running:
      python manage.py createsuperuser
      
    • Follow the prompts to set the email and password for the superuser.
  13. 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.
  14. 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.
  15. 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.
  16. 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.