Add a field to the User model

Hi all. The reason for my query is to ask how it is possible to add a field to the User model, from the auth_user table but keeping all the others and the functionality of the model. What I want to add is a field (a auth_user table column) called client_type.

But I’m not looking to apply a poxy model (since i need to add one more column to the table) or use another model and foreign key.
Is it possible to do this? I am currently doing it with a foreign key.

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


# Create your models here.
class User(AbstractUser):
    USER_TYPE_CHOICES = (
      (1, 'Assistant DAO'),
      (2, 'Chef Service Etude'),
      (3, 'Chef Departement Etude'),
      (4, 'Directeur Generale'),
      (5, 'admin'),
      (6, 'Coordinateur des Operations'),
      (7, 'Conducteurs des Travaux'),
      (8, 'Chef de Projet'),
      (9, 'DEGP'),
      (10, 'Magasinier'),
      (11, 'Intervenant'),
      )

    fonction = models.PositiveSmallIntegerField(
                  choices=USER_TYPE_CHOICES,
                  null=True
                  )
    avatar = models.ImageField(
                verbose_name='photo de profile',
                upload_to='media/avatars'
                )

    def __str__(self):
        fonction = "Admin"
        for loop in self.USER_TYPE_CHOICES:
            if loop[0] == self.fonction:
                fonction = loop[1]
                break
        return "{} : {}".format(self.username, fonction)

here I have an example.
in my project, I extended the AbstractUser class to add the attributes I want

> consult the documentation

It doesn’t matter whether you want to add 1 field or 100 fields, you choices for augmenting the User model fit into the same two categories.

See Extending the User model for your options.

2 Likes