[Django]-Django-queryset join without foreignkey

22👍

It’s possible to join two tables by performing a raw sql query. But for this case it’s quite nasty, so I recommend you to rewrite your models.py.

You can check how to do this here

It would be something like this:

from django.db import connection

def my_custom_sql(self):
    cursor = connection.cursor()    
    cursor.execute("select id_noga
                    from myapp_Tnogahist a
                    inner join myapp_Tdzien b on a.dziens=b.dziens
                    where b.dzienrok = 1234")
    row = cursor.fetchone()
    return row
👤Lucas

24👍

No joins without a foreign key as far as I know, but you could use two queries:

Tnogahist.objects.filter(dziens__in=Tdzien.objects.filter(dzienrok=1234))

8👍

To provide a little more context around @paul-tomblin’s answer,

It’s worth mentioning that for the vast majority of django users; the best course of action is to implement a conventional foreign key. Django strongly recommends avoiding the use of extra() saying "use this method as a last resort". However, extra() is still preferable to raw queries using Manager.raw() or executing custom SQL directly using django.db.connection

Here’s an example of how you would achieve this using django’s .extra() method:

Tnogahist.objects.extra(
    tables = ['myapp_tdzien'],
    where = [
        'myapp_tnogahist.dziens=myapp_tdzien.dziens',
        'myapp_tdzien.dzienrok=%s',
        ],
    params = [1234],
    )

The primary appeal for using extra() over other approaches is that it plays nicely with the rest of django’s queryset stack, like filter, exclude, defer, values, and slicing. So you can probably plug it in alongside traditional django query logic. For example: Tnogahist.objects.filter(...).extra(...).values('id_noga')[:10]

👤Aaron

7👍

Could you do this with .extra? From https://docs.djangoproject.com/en/dev/ref/models/querysets/#extra:

where / tables

You can define explicit SQL WHERE clauses — perhaps to perform
non-explicit joins — by using where. You can manually add tables to
the SQL FROM clause by using tables.

1👍

You could use following if you select some fields of two models without foreign key.

#import
from django.db.models import Subquery, OuterRef
from django.db.models.expressions import RawSQL

#result
dziens_result = Tdzien.objects.filter(dzienrok=1234).annotate(
                tno_dziens=Subquery(Tnogahist.objects.filter(dziens=OuterRef('dziens')).values('dziens')
                        )
                ).values_list('dziens', 'tno_dziens', 'dzienrok','id_noga')
👤peach

Leave a comment