django左外连接条件

时间:2016-06-03 05:21:18

标签: python django join left-join

如何在django中将条件置于左外连接查询中。我已在下面给出了示例查询。

我需要等效的django查询以下sql查询。

表1

class LeadGroups(Audit):

    user            = models.ForeignKey(User)
    group_name      = models.CharField(max_length=250)

表2

class lead(Audit):

 group        = models.ForeignKey(LeadGroups, null='true', blank='true')
 First_Name      = models.CharField(max_length=255, null='true', blank='true')
 Last_Name       = models.CharField(max_length=255, null='true', blank='true')

必填查询

SELECT "lead_leadgroups"."id", "lead_leadgroups"."created_by_id", "lead_leadgroups"."modified_by_id", "lead_leadgroups"."created_at", "lead_leadgroups"."modified_at", "lead_leadgroups"."customer_id", "lead_leadgroups"."user_id", "lead_leadgroups"."group_name", COUNT("lead_lead"."id") AS "totlacontact" FROM "lead_leadgroups"
LEFT OUTER JOIN "lead_lead" ON ( "lead_leadgroups"."id" = "lead_lead"."group_id" and **"lead_lead"."data_value" = True**) 
WHERE ("lead_leadgroups"."customer_id" = 309 ) GROUP BY "lead_leadgroups"."id", "lead_leadgroups"."created_by_id", "lead_leadgroups"."modified_by_id", "lead_leadgroups"."created_at", "lead_leadgroups"."modified_at", "lead_leadgroups"."customer_id", "lead_leadgroups"."user_id", "lead_leadgroups"."group_name" 
ORDER BY "lead_leadgroups"."id" ASC LIMIT 100

0 个答案:

没有答案