Django离开了连接模型

时间:2017-04-29 16:56:59

标签: python django django-models django-queryset

我想离开加入下面的表并在approved_coupon字段中添加过滤条件。

我的模特

class Voucher(models.Model):
    voucher_id = models.UUIDField(default=uuid.uuid4, editable=False)
    voucher_code = models.CharField()

class ApprovedCouponLine(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    approved_coupon = models.ForeignKey(ApprovedCoupon, on_delete=models.CASCADE, related_name='approved_coupon_lines')
    coupon_code = models.ForeignKey(Voucher, on_delete=models.CASCADE, related_name='approved_coupon_lines_coupons')

我尝试了这个,但它显示了内连接。

queryset = Voucher.objects.filter(approved_coupon_lines_coupons__approved_coupon_id='CD5FC4FE').values_list('code', flat=True)

当前查询:

SELECT "voucher_voucher"."code", "voucher_approvedcouponline"."id"  
FROM "voucher_voucher" 
INNER JOIN "voucher_approvedcouponline" ON ("voucher_voucher"."id" = "voucher_approvedcouponline"."coupon_code_id") 
WHERE "voucher_approvedcouponline"."approved_coupon_id" = 'CD5FC4FE'

预期查询:

SELECT "voucher_voucher"."code", "voucher_approvedcouponline"."id" 
FROM "voucher_voucher" 
LEFT JOIN "voucher_approvedcouponline" ON ("voucher_voucher"."id" = "voucher_approvedcouponline"."coupon_code_id" AND 
                                            "voucher_approvedcouponline"."approved_coupon_id" = 'CD5FC4FE'
                                      )

我在上面的例子中遗漏了什么?

0 个答案:

没有答案