Django ORM join without foreign keys and without raw queries

前端 未结 2 787

A

class AKeywords(models.Model):
    id = models.AutoField(primary_key=True, db_column=\"kw_id\")
    word = models.CharField(max_length=200)
    ...

             


        
2条回答
  •  悲哀的现实
    2021-01-13 19:48

    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"})
    

提交回复
热议问题