Django 模型继承了所有超级模型字段,除了 IntegerField
Django model is inheriting all super model Fields, except for IntegerField
我有一个奇怪的问题,我有一个模型的层次结构,一个抽象的用户 class:
class User(AbstractBaseUser):
user_name = models.CharField(max_length=32, unique=True)
email = models.EmailField(max_length=255, unique=True, null=True)
phone = PhoneNumberField()
access_token = models.CharField(max_length=255, unique=True, null=True)
notifications_token = models.CharField(max_length=255, unique=True, null=True)
photo = models.ImageField(null=True)
person_in_contact = models.CharField(max_length=32, null=True)
active = models.BooleanField(default=False)
confirmedEmail = models.BooleanField(default=False)
confirmedPhone = models.BooleanField(default=False)
completedProfile = models.BooleanField(default=False)
visible = models.BooleanField(default=True)
@property
def is_active(self):
return self.active
# def __str__(self):
# return "Client : " + self.user_name + " Email:" + self.email
def get_email(self):
return self.email
USERNAME_FIELD = 'user_name'
REQUIRED_FIELDS = ['user_name', 'phone', 'password']
class Meta:
abstract = True
然后一个人 class 和一家公司(这家公司没问题)class 从这家公司继承:
class Person(User):
GENDER = (('F', 'FEMALE'), ('M', 'MALE'))
name = models.CharField(max_length=50, null=True)
surname = models.CharField(max_length=50, null=True)
adress = models.CharField(max_length=255, null=True)
birth_date = models.DateField(null=True)
gender = models.CharField(max_length=1, choices=GENDER, null=True)
age = models.IntegerField(null=True)
def age(self):
today = date.today()
return today.year - self.birth_date.year
# def __str__(self):
# return super().__str__() + " Name : " + self.name
class Meta:
abstract = True
如您所见,IntegerField() 的唯一字段是年龄字段。
现在我有一个旅行者和一个 Driver class 继承自 class 的人,
问题是年龄字段,没有显示在数据库中,除非我在 classes 之一中覆盖它,这就是我所做的,我在旅行者中覆盖它所以它出现在数据库中,但没有'在 driver 中没有覆盖它,所以它没有显示。
旅行者:
class Traveller(Person):
photo = models.ImageField(null=True, upload_to='travellers/profile_pictures')
age = models.IntegerField(null=True)
class Meta:
verbose_name_plural = 'Travellers'
Driver:
class Driver(Person):
rating = models.DecimalField(default=0, decimal_places=1, max_digits=3)
driving_license = models.CharField(max_length=50, null=True)
insurance_number = models.CharField(max_length=50, null=True)
company = models.ForeignKey(TransportCompany, on_delete=models.DO_NOTHING, null=True)
photo = models.ImageField(null=True, upload_to='drivers/profile_pictures')
我需要知道如何解决这个问题,或者有什么问题,我们将不胜感激。
发生这种情况的原因是您的 age
字段与 age
函数同名。结果,age = ...
字段被 Python 忽略,取而代之的是 age
函数,因为那是您最后一次定义 age
变量。
出于同样的原因,age
字段在子类中弹出:您在那里定义了一个 age
变量,它优先于 "inherited" age 方法。
您应该重命名两者之一。例如:
class Person(User):
GENDER = (('F', 'FEMALE'), ('M', 'MALE'))
name = models.CharField(max_length=50, null=True)
surname = models.CharField(max_length=50, null=True)
adress = models.CharField(max_length=255, null=True)
birth_date = models.DateField(null=True)
gender = models.CharField(max_length=1, choices=GENDER, null=True)
<b>_age</b> = models.IntegerField(null=True)
def age(self):
today = date.today()
bod = self.birth_date
before_dob = (today.month, today.day) < (bod.month, bod.day)
return today.year - self.birth_date.year - before_dob
class Meta:
abstract = True
请注意,age
的计算并不完全准确:如果我们是在那一年的生日之前,则需要从年龄中减去一。
我有一个奇怪的问题,我有一个模型的层次结构,一个抽象的用户 class:
class User(AbstractBaseUser):
user_name = models.CharField(max_length=32, unique=True)
email = models.EmailField(max_length=255, unique=True, null=True)
phone = PhoneNumberField()
access_token = models.CharField(max_length=255, unique=True, null=True)
notifications_token = models.CharField(max_length=255, unique=True, null=True)
photo = models.ImageField(null=True)
person_in_contact = models.CharField(max_length=32, null=True)
active = models.BooleanField(default=False)
confirmedEmail = models.BooleanField(default=False)
confirmedPhone = models.BooleanField(default=False)
completedProfile = models.BooleanField(default=False)
visible = models.BooleanField(default=True)
@property
def is_active(self):
return self.active
# def __str__(self):
# return "Client : " + self.user_name + " Email:" + self.email
def get_email(self):
return self.email
USERNAME_FIELD = 'user_name'
REQUIRED_FIELDS = ['user_name', 'phone', 'password']
class Meta:
abstract = True
然后一个人 class 和一家公司(这家公司没问题)class 从这家公司继承:
class Person(User):
GENDER = (('F', 'FEMALE'), ('M', 'MALE'))
name = models.CharField(max_length=50, null=True)
surname = models.CharField(max_length=50, null=True)
adress = models.CharField(max_length=255, null=True)
birth_date = models.DateField(null=True)
gender = models.CharField(max_length=1, choices=GENDER, null=True)
age = models.IntegerField(null=True)
def age(self):
today = date.today()
return today.year - self.birth_date.year
# def __str__(self):
# return super().__str__() + " Name : " + self.name
class Meta:
abstract = True
如您所见,IntegerField() 的唯一字段是年龄字段。 现在我有一个旅行者和一个 Driver class 继承自 class 的人, 问题是年龄字段,没有显示在数据库中,除非我在 classes 之一中覆盖它,这就是我所做的,我在旅行者中覆盖它所以它出现在数据库中,但没有'在 driver 中没有覆盖它,所以它没有显示。 旅行者:
class Traveller(Person):
photo = models.ImageField(null=True, upload_to='travellers/profile_pictures')
age = models.IntegerField(null=True)
class Meta:
verbose_name_plural = 'Travellers'
Driver:
class Driver(Person):
rating = models.DecimalField(default=0, decimal_places=1, max_digits=3)
driving_license = models.CharField(max_length=50, null=True)
insurance_number = models.CharField(max_length=50, null=True)
company = models.ForeignKey(TransportCompany, on_delete=models.DO_NOTHING, null=True)
photo = models.ImageField(null=True, upload_to='drivers/profile_pictures')
我需要知道如何解决这个问题,或者有什么问题,我们将不胜感激。
发生这种情况的原因是您的 age
字段与 age
函数同名。结果,age = ...
字段被 Python 忽略,取而代之的是 age
函数,因为那是您最后一次定义 age
变量。
出于同样的原因,age
字段在子类中弹出:您在那里定义了一个 age
变量,它优先于 "inherited" age 方法。
您应该重命名两者之一。例如:
class Person(User):
GENDER = (('F', 'FEMALE'), ('M', 'MALE'))
name = models.CharField(max_length=50, null=True)
surname = models.CharField(max_length=50, null=True)
adress = models.CharField(max_length=255, null=True)
birth_date = models.DateField(null=True)
gender = models.CharField(max_length=1, choices=GENDER, null=True)
<b>_age</b> = models.IntegerField(null=True)
def age(self):
today = date.today()
bod = self.birth_date
before_dob = (today.month, today.day) < (bod.month, bod.day)
return today.year - self.birth_date.year - before_dob
class Meta:
abstract = True
请注意,age
的计算并不完全准确:如果我们是在那一年的生日之前,则需要从年龄中减去一。