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
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))
- [Django]-How to specify an IP address with Django test client?
- [Django]-How to make Django serve static files with Gunicorn?
- [Django]-For Django models, is there a shortcut for seeing if a record exists?
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]
- [Django]-Django model with 2 foreign keys from the same table
- [Django]-Django filter many to many field in admin?
- [Django]-How would you create a 'manual' django migration?
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.
- [Django]-Can't install via pip because of egg_info error
- [Django]-Could not find a version that satisfies the requirement pkg-resources==0.0.0
- [Django]-PyCharm Not Properly Recognizing Requirements – Python, Django
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')
- [Django]-Django 1.9 ImportError for import_module
- [Django]-How can I get tox and poetry to work together to support testing multiple versions of a Python dependency?
- [Django]-Is there a way to filter a queryset in the django admin?