vincentf
vincentf

Reputation: 1499

Django how do i filter ManyToMany field

I have the following M2M relationship between OrderPage and Site. How can I filter Site which belong to all OrderPage? Something like:

Site.objects.filter(all of the orderpage).distinct()

My model is

class OrderPage(models.Model):
    description = models.CharField(max_length=255, blank=False)
    sites = models.ManyToManyField(Site)

Upvotes: 0

Views: 109

Answers (1)

Selcuk
Selcuk

Reputation: 59445

The easiest way is to fetch all Site instances except those which don't have any OrderPages associated. For example:

Site.objects.all().exclude(orderpage__isnull=True)

Upvotes: 1

Related Questions