Menu Close

How to filter on foreign key properties with Python Django?

Sometimes, we want to filter on foreign key properties with Python Django.

In this article, we’ll look at how to filter on foreign key properties with Python Django.

How to filter on foreign key properties with Python Django?

To filter on foreign key properties with Python Django, we call filter with an argument with the name that includes the foreign key field name,

For instance, we write

Asset.objects.filter( project__name__contains="Foo" )

to call filter with the project table’s name field property including the string 'Foo'.

Conclusion

To filter on foreign key properties with Python Django, we call filter with an argument with the name that includes the foreign key field name,

Posted in Python, Python Answers