如何继承已经从 Django 中的 AbstractBaseUser 继承的 class
How to inherent from a class that is already inherited from AbstractBaseUser in Django
我正在尝试从已经从 AbstractBaseUser 继承的用户 class 进行继承,但我收到一个错误,告诉我 AUTH_USER_MODEL 引用了模型'authentication.User' 尚未安装 我确定我在 settings.py 中添加了 AUTH_USER_MODEL = 'authentication.User',我想知道如何才能成功通过此错误用户固有的 Class
这是代码:
from time import timezone
from django.contrib.auth.base_user import BaseUserManager, AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.db import models
class ResidentType(models.TextChoices):
Family = ('FM', 'Family')
Individual = ('IV', 'Individual')
class UserManager(BaseUserManager):
def create_user(self, username, first_name, last_name, email, password=None):
if username is None:
raise TypeError('User must have a username')
if email is None:
raise TypeError('User must have an Email')
if first_name is None:
raise TypeError('User must have a first_name')
if last_name is None:
raise TypeError('User must have a last_name')
user = self.model(username=username, first_name=first_name, last_name=last_name,
email=self.normalize_email(email), )
user.set_password(password)
user.save()
return user
def create_admin(self, username, first_name, last_name, email, password=None):
if username is None:
raise TypeError('User must have a username')
if email is None:
raise TypeError('User must have an Email')
if first_name is None:
raise TypeError('User must have a first_name')
if last_name is None:
raise TypeError('User must have a last_name')
admin = self.model(username=username, first_name=first_name, last_name=last_name,
email=self.normalize_email(email), )
admin.set_password(password)
Admin.admin = True
Admin.is_active = True
admin.save()
return admin
def create_syndic(self, username, first_name, last_name, email):
password = User.objects.make_random_password()
if username is None:
raise TypeError('User must have a username')
if email is None:
raise TypeError('User must have an Email')
if first_name is None:
raise TypeError('User must have a first_name')
if last_name is None:
raise TypeError('User must have a last_name')
syndic = self.model(username=username, first_name=first_name, last_name=last_name,
email=self.normalize_email(email), )
syndic.set_password(password)
Syndic.admin = True
Syndic.staff = True
Syndic.is_active = True
syndic.save()
return
def create_resident(self, username, first_name, last_name, email, password=None):
if username is None:
raise TypeError('User must have a username')
if email is None:
raise TypeError('User must have an Email')
if first_name is None:
raise TypeError('User must have a first_name')
if last_name is None:
raise TypeError('User must have a last_name')
resident = self.model(username=username, first_name=first_name, last_name=last_name,
email=self.normalize_email(email), )
resident.set_password(password)
Resident.admin = False
Resident.staff = False
Resident.is_active = False
resident.save()
return resident
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=255, unique=True, db_index=True)
first_name = models.CharField(max_length=255, db_index=True)
last_name = models.CharField(max_length=255, db_index=True)
email = models.EmailField(max_length=255, unique=True, db_index=True)
password = models.CharField(max_length=255, db_index=True)
is_active = models.BooleanField(default=False)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
objects = UserManager()
def __str__(self):
return self.email
def has_module_perms(self, app_label):
return True
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def has_perm(self, perm, obj=None):
return True
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
class Meta:
abstract = True
class Admin(User):
pass
class Syndic(User):
admin = models.ForeignKey(Admin, on_delete=models.CASCADE)
class Resident(User):
pass
type = models.CharField(max_length=2, choices=ResidentType, default=ResidentType.Family)
syndic = models.ForeignKey(Syndic, on_delete=models.CASCADE)
我认为问题在于将您的自定义用户模型设为抽象模型。
请重新检查、核实并确认。
对于以后遇到这种情况的任何人,
我已经通过使用代理模型而不是使用抽象 classes 解决了这个问题,您还可以从用户 class 中创建一对一的字段并将其用于其他 classes或者您可以简单地在用户模型中创建一个角色属性,而不是为它创建其他模型,并在您创建的每个视图中跟踪角色。
我正在尝试从已经从 AbstractBaseUser 继承的用户 class 进行继承,但我收到一个错误,告诉我 AUTH_USER_MODEL 引用了模型'authentication.User' 尚未安装 我确定我在 settings.py 中添加了 AUTH_USER_MODEL = 'authentication.User',我想知道如何才能成功通过此错误用户固有的 Class 这是代码:
from time import timezone
from django.contrib.auth.base_user import BaseUserManager, AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.db import models
class ResidentType(models.TextChoices):
Family = ('FM', 'Family')
Individual = ('IV', 'Individual')
class UserManager(BaseUserManager):
def create_user(self, username, first_name, last_name, email, password=None):
if username is None:
raise TypeError('User must have a username')
if email is None:
raise TypeError('User must have an Email')
if first_name is None:
raise TypeError('User must have a first_name')
if last_name is None:
raise TypeError('User must have a last_name')
user = self.model(username=username, first_name=first_name, last_name=last_name,
email=self.normalize_email(email), )
user.set_password(password)
user.save()
return user
def create_admin(self, username, first_name, last_name, email, password=None):
if username is None:
raise TypeError('User must have a username')
if email is None:
raise TypeError('User must have an Email')
if first_name is None:
raise TypeError('User must have a first_name')
if last_name is None:
raise TypeError('User must have a last_name')
admin = self.model(username=username, first_name=first_name, last_name=last_name,
email=self.normalize_email(email), )
admin.set_password(password)
Admin.admin = True
Admin.is_active = True
admin.save()
return admin
def create_syndic(self, username, first_name, last_name, email):
password = User.objects.make_random_password()
if username is None:
raise TypeError('User must have a username')
if email is None:
raise TypeError('User must have an Email')
if first_name is None:
raise TypeError('User must have a first_name')
if last_name is None:
raise TypeError('User must have a last_name')
syndic = self.model(username=username, first_name=first_name, last_name=last_name,
email=self.normalize_email(email), )
syndic.set_password(password)
Syndic.admin = True
Syndic.staff = True
Syndic.is_active = True
syndic.save()
return
def create_resident(self, username, first_name, last_name, email, password=None):
if username is None:
raise TypeError('User must have a username')
if email is None:
raise TypeError('User must have an Email')
if first_name is None:
raise TypeError('User must have a first_name')
if last_name is None:
raise TypeError('User must have a last_name')
resident = self.model(username=username, first_name=first_name, last_name=last_name,
email=self.normalize_email(email), )
resident.set_password(password)
Resident.admin = False
Resident.staff = False
Resident.is_active = False
resident.save()
return resident
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=255, unique=True, db_index=True)
first_name = models.CharField(max_length=255, db_index=True)
last_name = models.CharField(max_length=255, db_index=True)
email = models.EmailField(max_length=255, unique=True, db_index=True)
password = models.CharField(max_length=255, db_index=True)
is_active = models.BooleanField(default=False)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
objects = UserManager()
def __str__(self):
return self.email
def has_module_perms(self, app_label):
return True
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def has_perm(self, perm, obj=None):
return True
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
class Meta:
abstract = True
class Admin(User):
pass
class Syndic(User):
admin = models.ForeignKey(Admin, on_delete=models.CASCADE)
class Resident(User):
pass
type = models.CharField(max_length=2, choices=ResidentType, default=ResidentType.Family)
syndic = models.ForeignKey(Syndic, on_delete=models.CASCADE)
我认为问题在于将您的自定义用户模型设为抽象模型。
请重新检查、核实并确认。
对于以后遇到这种情况的任何人, 我已经通过使用代理模型而不是使用抽象 classes 解决了这个问题,您还可以从用户 class 中创建一对一的字段并将其用于其他 classes或者您可以简单地在用户模型中创建一个角色属性,而不是为它创建其他模型,并在您创建的每个视图中跟踪角色。