本文介绍了Django - 创建相当于“交叉连接”查询与Django ORM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要列出两个表的交叉产品。

我有两个Django模型是通过多对多关系相互关联的。 / p>

为了简单起见,我们希望这两个模型是比萨和Topping。



我希望查询返回如下所示:

  pizza_name topping 
---------------- -----
所有穿着的奶酪
所有穿着的蘑菇
所有穿洋葱
全部穿着peperoni
所有穿胡椒
reddit奶酪
reddit peperoni
reddit培根
reddit baconbits

数据量将非常大,我绝对需要使用计数和切片,所以我不能简单地迭代一个模型,并按照我的方式处理数据。



任何想法我应该如何进行?

解决方案

我最终通过简单的迭代中间体能够。


I have two Django models with are related to each other through a many-to-many relationship.

I need to list the cross product of both tables.

Let say for simplicity's sake that the two models are Pizza and Topping.

I would like the query to return something like this:

pizza_name    topping
---------------------
all dressed   cheese
all dressed   mushrooms
all dressed   onions
all dressed   peperoni
all dressed   pepper
reddit        cheese
reddit        peperoni
reddit        bacon
reddit        baconbits

The amount of data will be very large and I absolutely need to use count and slices so I can't simply iterate a model and treat the data as I go along.

Any idea how I should proceed?

解决方案

I eventually solved it by simply iterating on the intermediate table.

这篇关于Django - 创建相当于“交叉连接”查询与Django ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:34