[Fixed]-How can I get all the objects in a Django model that have a specific value for a ForeignKey field?

26👍

You can use a double underscore in the keyword passed to filter() to access fields in a foreign key relationship. Like this:

Item.objects.filter(parent__name="xyz")

Django documentation

👤Steef

2👍

1👍

Just for future reference for Googlers, with recent versions of Django you have to use an additional method in the keyword. For example, instead of parent__name you have to do parent__name__exact. Cato’s link contains other examples.

Leave a comment