Home > Software design >  Django: Custom User Model with Autoincrementing Id
Django: Custom User Model with Autoincrementing Id

Time:11-15

I am trying to use Django Authentication and I want to create a custom model for the user that has an autoincrementing integer as id. I know about uuid library, but I want the id to be an integer number, that is why I want to avoid it.

My code looks like:

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


class MyAccountManager(BaseUserManager):
    def create_user(self, first_name, last_name, email, username, avatar, password=None):
        if not username:
            raise ValueError('User must have an username')

        if not avatar:
            raise ValueError('User must have an avatar')

        user = self.model(
            email=self.normalize_email(email),
            username=username,
            avatar=avatar,
            first_name=first_name,
            last_name=last_name
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, first_name, last_name, email, username, avatar, password):
        user = self.create_user(
            email=self.normalize_email(email),
            username=username,
            avatar=avatar,
            password=password,
            first_name=first_name,
            last_name=last_name
        )
        user.is_admin = True
        user.is_active = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user


class Account(AbstractBaseUser):
    id = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    username = models.CharField(max_length=50, unique=True)
    email = models.CharField(max_length=50, unique=True)
    avatar = models.CharField(max_length=200)

    # required
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_superadmin = models.BooleanField(default=False)

    USERNAME_FIELD = 'id'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name', 'email', 'avatar']

    objects = MyAccountManager()

    def __str__(self):
        return self.username

    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, add_label):
        return True

The problem is that I still want to log in with the username, not the id. Also, as it autoincrements, it would be nice that I won't have to manually introduce it when I create a superuser from the console.

Is this possible?

CodePudding user response:

I tried to solve your requirement... using save method overriding

Model code

class CustomUserModel(AbstractBaseUser, PermissionsMixin):
    auto_id = models.PositiveBigIntegerField(unique=True)
    username = models.CharField(max_length=255, unique=True)
    email = models.EmailField(verbose_name="email address", max_length=255, unique=True)
    mobile_no = models.PositiveIntegerField(verbose_name="mobile number",unique=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    date_joined = models.DateTimeField(default=timezone.now)

    objects = CustomUserManager()

    USERNAME_FIELD = "username"
    REQUIRED_FIELDS = ['email','mobile_no']

    def save(self, *args, **kwargs):
        count_id = CustomUserModel.objects.all().count()
        self.auto_id = count_id 1
        super(CustomUserModel, self).save(*args, **kwargs)
    
    def __str__(self):
        return self.email

Output admin panel view

enter image description here

  • Related