gpt4 book ai didi

python - 关于继承以前模型的意见

转载 作者:行者123 更新时间:2023-11-28 18:10:42 26 4
gpt4 key购买 nike

我正在查看我的代码,发现这两个模型有多个相似的字段。我想知道我的 ParentProfile 模型是否可以继承我的 User 模型以缩短代码并消除重叠。这是一个非常初学者的问题,希望其他人对此事有意见。谢谢!

模型.py

class User(AbstractBaseUser):
email = models.EmailField(max_length=255, unique=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
child_first_name = models.CharField(max_length=255)
timestamp = models.DateTimeField(auto_now_add=True)
student = models.BooleanField(default=False)
parent = models.BooleanField(default=False)
teacher = models.BooleanField(default=False)
active = models.BooleanField(default=True) # can login
staff = models.BooleanField(default=False) # staff user, not superuser
admin = models.BooleanField(default=False) # superuser

objects = UserManager()

# takes email as username | removes email
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']

def __str__(self):
return self.email

def has_perm(self, perm, onj=None):
"Does the user have a specific permission?"
return True

def has_module_perms(self, app_label):
"Does the user have permissions to view the app 'app_label'?"
return True

@property
def is_staff(self):
return self.staff

@property
def is_admin(self):
return self.admin

@property
def is_active(self):
return self.active

class ParentProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
email = models.EmailField(max_length=255, unique=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
child_first_name = models.CharField(max_length=255)
timestamp = models.DateTimeField(auto_now_add=True)
student = models.BooleanField(default=False)
parent = models.BooleanField(default=False)
teacher = models.BooleanField(default=False)
active = models.BooleanField(default=True) # can login
staff = models.BooleanField(default=False) # staff user, not superuser
admin = models.BooleanField(default=False) # superuser

objects = UserManager()

# takes email as username | removes email
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'child_first_name']

def __str__(self):
return self.email

def has_perm(self, perm, onj=None):
"Does the user have a specific permission?"
return True

def has_module_perms(self, app_label):
"Does the user have permissions to view the app 'app_label'?"
return True

@property
def is_staff(self):
return self.staff

@property
def is_admin(self):
return self.admin

@property
def is_active(self):
return self.active

最佳答案

您可以创建具有公共(public)字段的抽象模型并在两者中继承

class CommonFieldModel(models.Model):
email = models.EmailField(max_length=255, unique=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
child_first_name = models.CharField(max_length=255)
timestamp = models.DateTimeField(auto_now_add=True)
student = models.BooleanField(default=False)
parent = models.BooleanField(default=False)
teacher = models.BooleanField(default=False)
active = models.BooleanField(default=True) # can login
staff = models.BooleanField(default=False) # staff user, not superuser
admin = models.BooleanField(default=False) # superuser

class Meta:
abstract = True

正如 Meta 类所定义的,它是一个抽象模型,Django 不会为此创建任何表。现在您可以在您的其他模型中继承此模型。例如。

class ParentProfile(CommonFieldModel):
user = models.OneToOneField(User, on_delete=models.CASCADE)
class User(CommonFieldModel, AbstractBaseUser):
user = models.OneToOneField(User, on_delete=models.CASCADE)

关于python - 关于继承以前模型的意见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50900153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com