Setting up a foreign key to an abstract base class with Django

前端 未结 2 1854
青春惊慌失措
青春惊慌失措 2021-01-01 09:28

I\'ve factored out common attributes from two classes into an abstract base class, however I have another model that needs to reference either one of those classes. It\'s no

2条回答
  •  渐次进展
    2021-01-01 10:02

    My gut would be to suggest removing the abstract modifier on the base class. You'll get the same model structure, but Answer will be it's own table. The downside of this is that if these are large tables and/or your queries are complex, queries against it could be noticeably slower.

    Alternatively, you could keep your models as is, but replace the ForeignKey to Answer with a GenericForeignKey. What you lose in the syntactic sugar of model inheritance, you gain a bit in query speed.

    I don't believe it's possible to reference an abstract base model by ForeignKey (or anything functionally the same).

提交回复
热议问题