[Answer]-Django unique filtering

1πŸ‘

βœ…

One way to do this is to filter directly on the Parent model:

parents = Parent.objects.filter(child__date_made__gte=a_while_ago).distinct()

You can also use a values or values_list query on recent, but that returns parent PKs rather than parent instances:

parent_pks = recent.values_list('parent', flat=True).distinct()

You could then use in_bulk to retrieve the actual instances:

parents = Parent.objects.in_bulk(parent_pks)

I would expect the first to be faster.

Leave a comment