Django模型继承基于属性值?

时间:2015-01-11 13:43:54

标签: python django django-models

很抱歉,我开始编写Django并需要帮助。

当parent属性为true时,我需要一个类(继承),因此当查找子类时,将所有保持此属性的内容都设置为true。

参见代码:

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

1 个答案:

答案 0 :(得分:1)

你只需要过滤你的Person查询:

customers = Person.objects.filter(customer=True)

您不需要其他课程