How to extend the default User Model of Django

You may find yourself in a situation where you need to extend the built-in User model to add additional fields or functionality. Fortunately, there are a few different ways you can do this, depending on your specific needs.

Using a Separate 1-1 Model

One way to extend the built-in User model is to use a one-to-one relationship. This allows you to create a separate model that contains the additional fields you need, and then link that model to the User model using a ForeignKey field. For example, if you wanted to add a "bio" field to the User model, you could create a separate Bio model with a ForeignKey field that links to the User model. This way, each user can have a unique bio associated with their account.

from django.db import models
from django.contrib.auth.models import User

class Bio(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    bio = models.TextField()

In this example, we've created a Bio model with a ForeignKey field that links to the User model. This allows us to add a bio field to the User model, without modifying the User model itself.

Extending the AbstractUser Model

Another way to extend the User model is to use a custom User model. This involves creating a new model that inherits from the built-in User model, and then adding the additional fields and functionality you need. This is a more advanced approach, but it allows you to completely customize the User model to meet the specific needs of your project.

from django.db import models
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    bio = models.TextField()

In this example, we've created a new model called CustomUser that inherits from the built-in AbstractUser model. This allows us to add a bio field to the User model, as well as any other fields or functionality we want to include.

Extending multiple User Models from a BaseUser Model

Finally, you can also use Django's abstract base classes to extend the User model. This involves creating an abstract base class that defines the additional fields and functionality you want to add, and then having your User model inherit from that class. This can be a good option if you need to add the same fields and functionality to multiple models, as it allows you to define them in a single, reusable place.

from django.db import models
from django.contrib.auth.models import AbstractBaseUser

class BaseUser(AbstractBaseUser):
    bio = models.TextField()

class Doctor(BaseUser):
    pass

class Patient(BaseUser):
    pass

In this example, we've defined an abstract base class called BaseUser that includes a bio field. We then create a CustomUser model that inherits from this base class, allowing us to add the bio field to the User model without having to define it in multiple places.

Overall, there are a few different ways you can extend the built-in User model in Django, depending on your specific needs. Whether you use a one-to-one relationship, a custom User model, or an abstract base class, you can easily add additional fields and functionality to the User model to make it work better for your project.

Previous
Previous

Developing a Python CLI Application that talks to Chat GPT

Next
Next

How to find the longest non-repeating substring in a string with python