How about, `set(Sample.objects.filter(results__assessment__isnull=False).values_list('id', flat=True))`