Django:自定义用户模型

7kqas0il  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(222)

我正在尝试使用Django身份验证,我想为用户创建一个自定义模型,它有一个自动递增的整数作为id。我知道uuid库,但我希望id是一个整数,这就是为什么我想避免它。
我的代码如下所示:

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

问题是我仍然希望使用用户名而不是ID登录。另外,由于它会自动递增,因此在从控制台创建超级用户时,我不必手动引入它。

这可能吗?

axzmvihb

axzmvihb1#

我尝试解决您的需求...使用保存方法覆写

型号:

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

输出管理面板视图:

相关问题