Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

350
Views
Django left join models

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?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!