Django 用户模型继承不起作用

Django User Model Inheritance not working

在我的 Django 应用程序中,我有两个模型 Retailer 和 Customer。两者的模型字段不同,但对于身份验证,通用字段是电子邮件和密码。因此,为此我使用了用户模型继承。这是我的代码看起来像

继承原因:我想实现令牌认证

models.py

class User(AbstractUser):
    
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def __str__(self):
        return self.email

class Retailer(User, PermissionsMixin):

    name = models.CharField(max_length=255)
    phoneNUMBER = models.CharField(max_length=10)
    verificationSTATUS = models.BooleanField(default=False)
    dateJOINED = models.DateField(default=timezone.now)

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'    
 
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name', 'phoneNUMBER',]
 
    objects = RetailerManager()
 
    def __str__(self):
        return self.name
 
class Customer(User, PermissionsMixin):

    name=models.CharField( max_length=255, null=False,)
    phone_Number=models.CharField( max_length=10, null=False)
    address=models.TextField(max_length=255,null=False)
    pin_Code=models.CharField( max_length=6,null=False )
    anniversary_Date=models.DateField(blank=True,null=True)
    hobbies=models.TextField(blank=True,null=True)
    profession=models.CharField(blank=True,max_length=20,null=True,)
    created_At=models.DateField(default=timezone.now)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name', 'phoneNUMBER', 'address', 'pin_Code']    

    objects = CustomerManager()

    def __str__(self):
        return (self.name)   

managers.py

class UserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError(_('The Email must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(email, password, **extra_fields)
 

class RetailerManager(BaseUserManager):
 
    def create_retailer(self, name, email, phoneNUMBER, password=None):
        if email is None:
            raise TypeError('User must have an email address.')
        retailer = Retailer(
            email=self.normalize_email(email),
            name=name,
            phoneNUMBER=phoneNUMBER
            )
        retailer.set_password(password)
        retailer.save()
        return retailer

class CustomerManager(BaseUserManager):
 
    def create_customer(self, name, email, phone_Number, address, pin_Code, password=None, **extra_fields):
        if email is None:
            raise TypeError('User must have an email address.')
        customer = Customer(
            email=self.normalize_email(email),
            name=name,
            phone_Number=phone_Number,
            address=address,
            pin_Code=pin_Code,
            )
        customer.set_password(password)
        customer.save()
        return customer

当我运行这个

 Retailer.objects.create(email="johndoe@gmail.com", name="John Doe", password="john@123", phoneNUMBER = "90909090")

我收到这个错误

django.db.utils.IntegrityError: UNIQUE constraint failed: retailer_user.username

此外,当我 运行 这个

Customer.objects.create(email="janedoe@gmail.com", name="Jane Doe", password="jane@123")

我得到同样的错误

django.db.utils.IntegrityError: UNIQUE constraint failed: retailer_user.username

谁能告诉我哪里做错了?这是最好的方法吗?另外,我如何为零售商和客户实施令牌身份验证?

简单来说,你只需使用 @login_required 装饰器。

@login_required(login_url='/example url you want redirect/') #redirect when user is not logged in
def myview(request):
    do something
    return something #returns when user is logged in

您需要从 AbstractBaseUser 而不是 AbstractUser 继承 User 才能使用 email 而不是 username 的身份验证:

引发错误的 username 字段来自 AbstractUser class.

class User(AbstractBaseUser):
    
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(_('first name'), max_length=150, blank=True)
    last_name = models.CharField(_('last name'), max_length=150, blank=True)
    email = models.EmailField(_('email address'), blank=True)
    is_staff = models.BooleanField(
        'staff status',
        default=False,
        help_text='Designates whether the user can log into this admin site.',
    )
    is_active = models.BooleanField(
        'active',
        default=True,
        help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.',
    )
    date_joined = models.DateTimeField('date joined', default=timezone.now)
    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = ['email']

    objects = UserManager()

    def get_full_name(self):
        """
        Return the first_name plus the last_name, with a space in between.
        """
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        """Return the short name for the user."""
        return self.first_name

    def __str__(self):
        return self.email