How to "left join on a.id = b.id and a.xxx = b.xxx" in queryset?

class TA(models.Model):
    col1 = models.CharField()
    col2 = models.CharField()
    x_number = models.IntegerField()

class TB(models.Model):
    col1 = models.CharField()
    col2 = models.CharField()
    ta_id= models.ForeignKey(TA)
    x_number = models.IntegerField()

The query I seek is in this form.

select *
  from TA
  left join TB
    on TA.id = TB.ta_id
   and TA.x_number = TB.x_number
where TA.deleted = False
  and  TB.deleted = False
  • TA.deleted and TB.deleted are Boolean Fields.
  • TA.id are primary key.

How do I create a query like this in Django?

Your models don’t match your example.

Are TA.id and TB.id references to the inherant primary keys of each table? If not, what are they?

Are TA.deleted and TB.deleted BooleanFields?

You also show TB as having a foreign key to TA, yet your example makes no use of it. Are you trying to do a join here ignoring the foreign key?

thank you for your reply.
The question was incorrect, so I corrected it.