Django: Two different child classes point to same parent class

前端 未结 4 1141
名媛妹妹
名媛妹妹 2021-02-16 00:15

I have a model Person which stores all data about people. I also have a Client model which extends Person. I have another extending model OtherPe

4条回答
  •  生来不讨喜
    2021-02-16 00:31

    What you are doing is not possible as you do it, Django has specific rules for inheritance

    The only possible schema is:

    class Parent(models.Model):
        class Meta:
            abstract = True # MUST BE !!! This results in no relation generated in your DB
    
        field0 = models.CharField(...
        ...
    
        # here you're allowed to put some functions and some fields here
    
    
    class Child(models.Model):
        field1 = models.CharField(...
        ...
    
        # Anything you want, this model will create a relation in your database with field0, field1, ...
    
    
    class GrandChild(models.Model):
        class Meta:
            proxy = True # MUST BE !!! This results in no relation generated in your DB
    
        # here you're not allowed to put DB fields, but you can override __init__ to change attributes of the fields: choices, default,... You also can add model methods.
    

    This is because there is no DB inheritance in most DBGS. Thus you need to make you parent class abstract !

提交回复
热议问题