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
Source:stackexchange.com