Can a django model have two abstract classes?

梦想的初衷 提交于 2021-02-07 21:53:00

问题


I have this models:

class BillHeader(models.Model):
    billno = models.IntegerField(primary_key=True, blank=True)

class BillData(models.Model):
    price = models.DecimalField(_('Price'), max_digits=12, decimal_places=2)
    amount = models.DecimalField(_('Amount'), max_digits=6, decimal_places=2)

    [... rest of the model ...]

    class Meta:
        abstract = True

class BillFooter(models.Model):
    total = models.DecimalField(_('Total'), max_digits=12, decimal_places=2)

    [... rest of the model ...]

    class Meta:
        abstract = True

BillData and BillFooter are common to every BillHeader so I've marked them as abstract. Can I do class BillHeader(BillData, BillFooter) or I'm doing something wrong?

I also thought about doing BillData the main one, and BillHeader BillFooter abstract. I don't have any experience on doing data models (at least not complex ones) and I'm a bit lost. What would you recommend?


回答1:


Yes, a Django model can inherit from as many abstract base classes as you like, as long as they don't result in an ambiguous "model resolution order". Think of inheritance as a chain... each class you inherit from is a link in the chain. Inheriting from two base classes is just adding two links to the chain instead of one.

In other words, if your abstract base classes inherit from models.Model, then don't try to inherit from both the abstract base class and models.Model in your Bill class. models.Model is already in the inheritance chain, so inheriting from it causes chaos in the chain of base classes.

As to how I would structure these classes, I would create a model called Bill that inherited from BillHeader, BillData, and BillFooter. The reason for this is that I like my Django models to represent discrete objects (e.g. Bill, Article, BlogPost, Photo, etc.)

However, the point of abstract base classes is to be able to add a level of abstraction to common fields and methods so that multiple classes can inherit from them. If you're just creating a Bill class it's somewhat meaningless. If, however, you had Bill, and UnpaidBill, and PaidBill... all of those would have common fields that should appear on all of them and you can save yourself a lot of trouble by abstracting to an ABC.

Hopefully that offers a little insight to what ABC's and inheritance are good for.



来源:https://stackoverflow.com/questions/3144019/can-a-django-model-have-two-abstract-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!