3👍
✅
This will work:
XKeywords.objects.filter(pk_id=my_id).extra(select={'word':'SELECT word FROM "A"."Keywords" WHERE "public"."XKeywords".k_id = "A"."Keywords".kw_id'})
or
raw_sql = """SELECT * FROM (SELECT * FROM "public"."XKeywords" WHERE pk_id = my_id) as "XK" LEFT OUTER JOIN "A"."Keywords" as "AK" ON "AK".kw_id = "XK".k_id ;"""
XKeywords.objects.raw(raw_sql)
This is an workaround i was expecting something more “clever”. It would be nice to have something more directly like:
XKeywords.objects.filter(pk_id=my_id).join(k_id=A.kwd,from={"AKeywords":"A"})
23👍
The current way to do this is with a Subquery
and an OuterRef
:
Example taken from my code. Store
and StoreInformation
have a field called store_number
.
from django.db.models import Subquery, OuterRef
Store.objects.annotate(timezone=Subquery(
StoreInformation.objects.filter(store_number=OuterRef('store_number')).values('store_timezone')[:1]
))
This is joining to add a field called store_timezone
.
- Django Queryset to dict for use in json
- Change row colour in Django Admin List
- Django GROUP BY field value
- How do you update a django template context variable after an AJAX call?
- Is there a way to generate pdf containing non-ascii symbols with pisa from django template?
Source:stackexchange.com