Question: I have models that look something like this:

```py
class Type(models.Model):
    name = models.CharField(max_length=50)
    notes = models.CharField(max_length=50)

class Instance(models.Model):
    type = models.ForeignKey(Type, on_delete=models.CASCADE)
    color = models.CharField(max_length=50)
    quantity = models.PositiveIntegerField()

```

I want to get a list of all `Type`s that have at least one `Instance` whose `quantity` is greater than 0. How can I express that?

In (pseudo) vanilla Python, this would be something like:

```py
[type for type in Type.objects.all()
       if type.instance_set.objects.filter(lambda instance: instance.quantity > 0)]

```

I tried

```py
available_types = Type.objects.filter(Q(instance_set__contains=Q(quantity__gt=0))

```

but this doesn't work because Django is looking for `quantity` as an attribute of `Type`, and of course doesn't find it, because it's an attribute of `Instance`.

Answer: 
You can filter directly across a relationship:

```
Type.objects.filter(instance__quantity__gt=0)

```
why not just query the Instance objects for instance.quantity > 0 and then use values\_list distinct notation? 

Something along the lines of:

```
types_with_quantity_gt_zero = Instance.objects.filter(quantity__gt=0).values_list('type', flat=True).distinct()

```