django extend user model

To extend the user model in Django, you can follow these steps:

  1. Create a new app: Start by creating a new Django app where you will define your custom user model. You can use the following command in the terminal:

python manage.py startapp <app_name>

  1. Define a new user model: Open the models.py file in your newly created app and define a new user model by subclassing the AbstractUser class provided by Django. This will give you access to all the fields and methods of the default user model. You can add additional fields specific to your application by defining them in your custom user model.

```python from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser): # Add your custom fields here pass ```

  1. Update the settings: Open the settings.py file of your Django project and locate the AUTH_USER_MODEL setting. Update its value to point to your new custom user model.

python AUTH_USER_MODEL = '<app_name>.CustomUser'

  1. Create database migrations: Run the following command in the terminal to create the initial database migrations for your new user model:

python manage.py makemigrations

  1. Apply database migrations: Apply the newly created migrations to your database by running the following command:

python manage.py migrate

  1. Update references: Finally, update any references to the default user model throughout your Django project. This includes updating any foreign keys or many-to-many relationships that were referencing the default user model to now reference your custom user model.

By following these steps, you will be able to extend the user model in Django and add your own custom fields and functionality.