[Answered ]-Django: filter ManyToMany join

2๐Ÿ‘

A ManyToMany field is already a queryset, so if you want the active subscribers you can just call its filter method, perhaps via a method in the List class. The through table is available for filtering in the same way as the target table:

class List(models.Model):
    # ... etc ...
    @property
    def active_subscribers(self):
        return self.subscribers.filter(listsubscription__is_active = True)

To return lists with at least one active subscriber, use this query:

List.objects.filter(listsubscription__is_active = True)
๐Ÿ‘คGareth Rees

Leave a comment