Django多表继承和左外部联接 [英] Django Multi-Table-Inheritance and Left Outer Joins

查看:133
本文介绍了Django多表继承和左外部联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,对于模型继承,我似乎对Django来说是一个普遍的问题.我有一堆想要单独显示或作为一组显示的不同模型.读取方式:查询数据库中的所有内容,或仅查询特定类别(模型)的项目.

Recently I had what seems to be a common question for Django regarding model inheritance. I have a bunch of different models that I want to display either individually or as a group. Read as: query the database for everything in it, or just a certain category (model) of items.

最终,我决定采用多表继承.我的模型如下所示:

Ultimately, I settled on multi-table-inheritance. My models look something like:

class Unit(models.Model):
    etc, etc...

class Child1(Unit):
    etc, etc...

class Child2(Unit):
    etc, etc...

因此,现在我可以查询单位以获取所有内容,并可以查询单个孩子以获取更具体的信息.它工作正常,但我只是查看了为所有单元列表生成的SQL ...它变得很难看!

So now I can query units to get everything and individual children to get more specific. It works fine, but I just looked at the SQL generated for a list of all units...it gets pretty ugly!

SELECT "all the fields from all the tables (Unit and Children)" 
FROM "Unit" 
LEFT OUTER JOIN "Child1" ON ("whatever") 
    LEFT OUTER JOIN "Child2" ON ("whatever") 
        LEFT OUTER JOIN "Child3" ON ("whatever") 
            LEFT OUTER JOIN "Child4" ON ("whatever") 
                LEFT OUTER JOIN "Child5" ON ("whatever")

基本上,当我获得所有项目的索引视图时,Unit的每个子级都需要另一个左外部联接.

Basically, each child of Unit will require another left outer join when I get an index view of all items.

假设我最多有5个Unit的子级,并且DB中最多有200个项目,这是否会破坏交易?我总是可以只缓存索引视图,是吗?还是我错过了MTI的另一个问题?本书"2 Scoops of Django"绝对反对使用多表继承……以至于说永远不要这样做".但是我觉得它解决了我的问题,很容易理解,并且考虑到Django处理关系的方式,这几乎是必不可少的.

Assuming I have at most 5 children of Unit and that there will be at most 200 items in the DB, is this a deal-breaker? I can always just cache the index view, yes? Or am I missing another problem with MTI? The book "2 Scoops of Django" is absolutely against using multi-table-inheritance...to the point of saying "don't do it ever". But I feel like it solves my problem, is easily understood, and is almost a necessary evil given how Django handles relations.

保留我所拥有的东西还是回到绘图板上?

Keep what I have or go back to the drawing board?

推荐答案

如果您的问题是加载不需要的数据,则可以使用

If your problem is loading data that you don't need, you can use the only method to retrieve only the data that you need from the database.

如果问题在某种程度上是这样的:

If the problem is somewhat in this shape:

您有一个始终带有值的模型,但是它可以具有几个可能会或可能不会填写的不同配置文件".

You have one model that always have values in, but it can have a couple of different "profiles" that might or might not be filled in.

我认为您的身体状况良好,我的意思是,替代方法是使所有内容均null可用,这不好,或者使用ForeignKeys,这将导致相同的查询.我唯一要注意的是,只有在对要使用的实体有意义的情况下,才应使用模型继承,否则可以使用

I think you are in good shape, I mean, the alternative is to have everything be nullable, which isn't nice, or to use ForeignKeys, which is gonna result in the same sort of query. The only note that I have is that you should use model inheritance only if it makes sense for the entities that you are working with, otherwise you can use a OneToOne field (this is only conceptually because inheritance uses a OneToOne field under the hood)

这篇关于Django多表继承和左外部联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆