Django model subclassing: Get the subclass by querying the superclass

后端 未结 4 1614
渐次进展
渐次进展 2020-12-16 23:01

The following code is given:

class BaseMedium(models.Model):
    title = models.CharField(max_length=40)
    slug = models.SlugField()

class A(BaseMedium):
         


        
4条回答
  •  失恋的感觉
    2020-12-16 23:24

    You should check the solution posted by Carl Meyer some time ago. It internally uses the ContentType approach, but it encapsulates it very elegantly .

    He also points to an alternative, and more efficient solution, that doesn't need to store an aditional field on the database, but it will only work for direct child classes. If you have several inheritance levels, the first solution is better.

提交回复
热议问题