I have the following models:
class Partner(models.Model):
partner_name = models.CharField(max_length=50)
class Allocation(models.Model):
partner = models.ForeignKey(
Partner,
related_name='partner_allocations',
on_delete=models.CASCADE
)
is_allocated = models.BooleanField(default=False)
By using Django ORM, I need the joined tables with columns as ['partner_name', 'partner_id', 'is_allocated'].
All partner names(unrelated too), and related data from Allocation model.
SQL = SELECT partner.id, allocation.partner_id, allocation.is_allocated
FROM Partner LEFT OUTER JOIN Allocation ON Partner.id=Allocation.partner_id
or
SQL = SELECT partner.id, allocation.partner_id, allocation.is_allocated
FROM Allocation RIGHT OUTER JOIN Partner ON Allocation.partner_id=Partner.id
The output for the above SQL using Django ORM.
The syntax for SQL queries might be wrong here, but they give the overall idea.
I want to use raw SQL only as the last option.
Please, can someone help.