Cross join in Django ORM

Hi everybody!

Is there any way to perform cross join (full cartesian) in Django ORM for 2 or more unrelated models? The reqult should be queryset, so I could apply filtering, annotation, e t.c. al usual.

Example for undependent models region and week:
values = region.objects.cross_join(week).filter(…).values_list(‘region_id’, ‘week_id’)
Resulting SQL: SELECT, FROM region CROSS JOIN week

So I need something like “cross_join”, but I cannot find it anywhere in Django ORM. I found qs.query.join, but it’s not what satisfies me.
Pandas using should be good altertative, but I prefer to stay with Django ORM.


Hi @snb2013! While I don’t have the answer to your inquiry, this other topic seems related:

There are also a few stackoverflow and reddit matches when using a search engine. I hope this helps!

(Emphasis added)

There is one way I can think of right off-hand - create a view in the database to perform the join, a model in Django to correspond to that view, and then use the ORM for the remainder of the operations.

There are probably better ways …