Skip to content

Relationship Spanning

The field name used in a filter can also be written to span across relationships.

For example, if you want to find all Sites which belong to a Project named "My Project", you can write the filter ?project__name=My Project like the below

List Sites > Which belong to a Project > Which has the name "My Project"

/api/projects/sites/?project__name=My Project

In the above case, you are not searching for the relationships based on the default serialized field of the related object (id), but based on a different field on that related object (name).

Relationship spans can jump through as many different objects as you want. Like from Locations all the way to a Project's name field:

List Locations > Which belong to a Site > Which belongs to a Project > Which has the name "My Project"

/api/projects/locations/?site__project__name=My Project

Including Lookups

Don't forget that there is an implicit exact lookup included in the spanned relationship examples above. If you want to use a specific lookup, you can add it to the filter:

List Locations > Which belong to a Site > Which belongs to a Project > Which has a name containing the text "Test"

/api/projects/locations/?site__project__name__contains=Test