Q:

Filter Django objects where related object exists

This is bound to be a duplicate question but I can't find any others. I'm trying to get a list of photos that have complaints. I can't simply get complaints and deal with the related photos - I need a queryset of photos.
This should work but doesn't seem right:
Photo.objects.filter(complaint__id__gte=0)

This doesn't seem like the most efficient way:
Photo.objects.annotate(Count('complaint')).exclude(complaint__count=0)

Is there a better way?

A:

how about ...
Photo.objects.filter(complaint__isnull=False)
from https://docs.djangoproject.com/en/dev/topics/db/queries/

A:

I'm not sure which variant is the best, but that works as well.
Photo.objects.exclude(complaint=None)
Generated SQL query here is not the same as in case with .filter(complaint__isnull=False), but sense is identical.

