How to query abstract-class-based objects in Django?

前端 未结 7 1848
自闭症患者
自闭症患者 2020-12-13 17:11

Let\'s say I have an abstract base class that looks like this:

class StellarObject(BaseModel):
  title = models.CharField(max_length=255)
  description = mod         


        
相关标签:
7条回答
  • 2020-12-13 17:56

    That seems horribly inefficient. Is that the only way?

    As far as I know it is the only way with Django's ORM. As implemented currently abstract classes are a convenient mechanism for abstracting common attributes of classes out to super classes. The ORM does not provide a similar abstraction for querying.

    You'd be better off using another mechanism for implementing hierarchy in the database. One way to do this would be to use a single table and "tag" rows using type. Or you can implement a generic foreign key to another model that holds properties (the latter doesn't sound right even to me).

    0 讨论(0)
提交回复
热议问题