[Django]-Django Filter Query Foreign Key

57👍

If you want to get Publishers, you have to start with Publisher. That means you have to query through the Book → Publisher relation backwards. Here’s what the docs say about it:

Lookups that span relationships

To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want

To refer to a “reverse” relationship, just use the lowercase name of the model.

The query:

Publisher.objects.filter(book__title__startswith='hello')

Leave a comment