开发者

Django: How to reduce inner joins on Model inheritance?

开发者 https://www.devze.com 2023-01-30 15:56 出处:网络
I have several models inheriting from a base model. The fields in the base model are needed rarely, but Django keeps doing complex inner joins to retrieve those fields whenever I use any of theinherit

I have several models inheriting from a base model. The fields in the base model are needed rarely, but Django keeps doing complex inner joins to retrieve those fields whenever I use any of the inherited models. How can I tell Django to avoid this ? I only need the fields in this model rarely.

Note: maybe only(..) would work(I didn't check), but I would开发者_开发技巧 need to add it in many places in the code..


Use abstract model inheritance.

In short, setting abstract = True in the base class' meta, makes Django using abstract inheritance, meaning each derived model will contain a copy of all the fields defined in the base model.

By the way, one of the Django's maintainers, Jacob Kaplan-Moss has quite a strong opinion against concrete inheritance,

model inheritance also offers a really excellent opportunity to shoot yourself in the foot: concrete (multi-table) inheritance

and again:

I’d strongly suggest that Django users approach any use of concrete inheritance with a large dose of skepticism.

Personally, I have never had to use model inheritance at all; however, after reading that blog entry, I am quite convinced in trying to avoid concrete inheritance as much as possible.


I'd say the only possiblity to avoid this is either making your base class abstract, or you create some custom sql queries that don't hit the 'base'-table...

0

精彩评论

暂无评论...
验证码 换一张
取 消