[Answer]-How to get single related object after filter

1👍

You can use prefetch_related

Parent.objects.filter(children__first=True).prefetch_related('children')

Or select_related

Child.objects.filter(first=True).select_related('parent')

0👍

You have to use the fetch_related method for that and do the lookup the other way around unfortunately.

In this case it should be something like this:

first_children = Child.objects.filter(first=True).select_related('parent')
parents = [c.parent for c in first_children]
👤Wolph

Leave a comment