# Cross join in Django ORM

**URL:** <https://forum.djangoproject.com/t/cross-join-in-django-orm/31567>\
**Category:** Using the ORM\
**Created:** [May 27, 2024, 7:03pm UTC](https://forum.djangoproject.com/t/cross-join-in-django-orm/31567 "2024-05-27T19:03:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snb2013](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/snb2013/32/21567_2.png) [@snb2013](https://forum.djangoproject.com/u/snb2013)\
**Post date:** [May 27, 2024, 7:03pm UTC](https://forum.djangoproject.com/t/cross-join-in-django-orm/31567/1 "2024-05-27T19:03:22Z")

</div>

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 region.id, week.id 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.

Thanks.

---

<div class="post-metadata">

**Author:** ![nessita](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nessita/32/12194_2.png) [@nessita](https://forum.djangoproject.com/u/nessita)\
**Post date:** [May 30, 2024, 12:50pm UTC](https://forum.djangoproject.com/t/cross-join-in-django-orm/31567/2 "2024-05-30T12:50:43Z")

</div>

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

> [@generate series & cross join](https://forum.djangoproject.com/t/generate-series-cross-join/30080):
>
> Hello ! I am trying to generate an initial queryset consisting of a CROSS JOIN between a generated date sequence using Postgres generate\_series and another table Foo (resulting in a generated table containing each item of Foo for each date, the cartesian product). I would like to use the Django ORM because I have a lot of post-processing to do after that, with filters, annotations, sub-queries, aggregations, etc. I found a raw SQL answer to a very similar question here: And a library implem…

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 30, 2024, 1:30pm UTC](https://forum.djangoproject.com/t/cross-join-in-django-orm/31567/3 "2024-05-30T13:30:48Z")

</div>

> [@snb2013](#):
>
> Is there **any** way to perform cross join …

(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 …
