Categories
Python Answers

How to do a left join with the Python Django ORM?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *