I want to left join the below tables and add a filter condition on approved_coupon field.
My models
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')
I tried this, but it shows the inner join.
queryset = Voucher.objects.filter(approved_coupon_lines_coupons__approved_coupon_id='CD5FC4FE').values_list('code', flat=True)
Current Query:
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'
Expected Query:
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'
)
What I missed in the above example?