Django - Create the equivalent of a “cross-join” query with the Django ORM

谁说胖子不能爱 提交于 2019-12-25 04:16:13

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7894214/django-create-the-equivalent-of-a-cross-join-query-with-the-django-orm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!