2015-01-17 89 views
0

當父屬性爲true時,我需要一個類(繼承),因此何時需要查找子類,將所有包含此屬性的屬性設置爲true。 之後,我將需要在管理面板中使用這個新類並作爲外鍵。例如:CustomerServices基於屬性值的Django模型繼承是可能的嗎?

看到代碼:

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.name 

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 = ["name"] 

class Customer(Person): 
    # I want to consider all Person object with customer=True 

# I need this working and available in the Django Admin like others classes 
class CustomerService(Customer): 
    service_name... 
    service_qtty... 
    service_price... 
    ... 
+0

任何其他地方是的,這是可能的,但看起來像不好的設計決策。你想解決什麼樣的問題? – catavaran

回答

0

你並不需要有一個單獨的模型。您可以從Person類中提取所有客戶。即Person.objects.filter(customer = True)。 添加django的ObjectManagers的性感,你可以做類似的事情。

class PersonManager(models.Manager): 
    def customers(self): 
     return self.filter(customer = True) 

class Person(models.Model): 
    ... 
    objects = PersonManager() 
在你的意見

再或者你可以這樣調用它

customers = Person.objects.customers()