Sometimes, we want to do a left join with the Python Django ORM.
In this article, we’ll look at how to do a left join with the Python Django ORM.
How to do a left join with the Python Django ORM?
To do a left join with the Python Django ORM, we can call filter with the related table field’s is_null argument set to True.
For instance, we write
Department.objects.filter(departmentvolunteer__department__isnull=True)
to call filter with the departmentvolunteer__department__isnull set to True to do a left join on the departmentvolunteer table by making the foreign key optional.
Conclusion
To do a left join with the Python Django ORM, we can call filter with the related table field’s is_null argument set to True.