[Answered ]-Django: Custom User Model with Autoincrementing Id

1👍

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

Leave a comment