基于属性值的Django模型继承?
Django model inheritance based on atribute value?
抱歉,我刚开始使用 Django 编程,需要帮助。
当父属性为真时,我需要一个 class(继承),因此在寻找子属性时 class,将所有拥有此属性的属性都设为真。
见代码:
class Person(models.Model):
name = models.CharField(max_length=200)
active = models.BooleanField(default=True)
customer = models.BooleanField(default=False)
employee = models.BooleanField(default=False)
vendor = models.BooleanField(default=False)
def __str__(self):
return self.nome
class People(Person):
registration_number = models.CharField(max_length=14)
birth_date = models.DateField()
sex = models.CharField(max_length=1)
class Company(Person):
registration_number = models.CharField(max_length=20)
foundation_date = models.DateField(blank=True, null=True)
class Meta:
ordering = ["nome"]
class Customer(Person):
# I want to consider all Person object with customer=True
您只需像这样过滤您的个人查询:
customers = Person.objects.filter(customer=True)
你不需要另一个 class
抱歉,我刚开始使用 Django 编程,需要帮助。
当父属性为真时,我需要一个 class(继承),因此在寻找子属性时 class,将所有拥有此属性的属性都设为真。
见代码:
class Person(models.Model):
name = models.CharField(max_length=200)
active = models.BooleanField(default=True)
customer = models.BooleanField(default=False)
employee = models.BooleanField(default=False)
vendor = models.BooleanField(default=False)
def __str__(self):
return self.nome
class People(Person):
registration_number = models.CharField(max_length=14)
birth_date = models.DateField()
sex = models.CharField(max_length=1)
class Company(Person):
registration_number = models.CharField(max_length=20)
foundation_date = models.DateField(blank=True, null=True)
class Meta:
ordering = ["nome"]
class Customer(Person):
# I want to consider all Person object with customer=True
您只需像这样过滤您的个人查询:
customers = Person.objects.filter(customer=True)
你不需要另一个 class