4
I was racking my brain to do union
of two darlings using Django Filter and I ended up doing it on hand (Model.objects.raw("SELECT....")
).
How could I union
with Django’s ORM?
Note: I have 2 different tables with some common fields.
Table1
- table1_id
- name
- Description
- created_at
- user_id
Table2
- table2_id
- name
- Description
- created_at
- user_id
The relationship I want to maintain is between user_id
. I want to put these records together and sort through created_at
.
Just to be clear,
UNION
you mean literally joining the results of one query with the other?– Leonardo Pessoa
Yes, I have 2 different tables with some fields in common. I will edit in the topic.
– Guilherme IA
I guess you’d have to make one for, like,:
for obj in Table1.objects.all(): result += Table2.objects.filter(user_id=obj.user_id)
. Works?– Leonardo Pessoa
Between using 2 selects and a
.raw
I think the.raw
right?– Guilherme IA
In terms of performance, believe yes. It is worth taking the test. I think it will depend on the size of your table as well.
– Leonardo Pessoa