Ska
Ska

Reputation: 6888

Django filter the model on ManyToMany count?

Suppose I have something like this in my models.py:

class Hipster(models.Model):
  name = CharField(max_length=50)

class Party(models.Model):
  organiser = models.ForeignKey()
  participants = models.ManyToManyField(Profile, related_name="participants")

Now in my views.py I would like to do a query which would fetch a party for the user where there are more than 0 participants.

Something like this maybe:

user = Hipster.get(pk=1) 
hip_parties = Party.objects.filter(organiser=user, len(participants) > 0)

What's the best way of doing it?

Upvotes: 89

Views: 28295

Answers (5)

George Oyosa
George Oyosa

Reputation: 49

I use the following method when trying to return a queryset having at least one object in a manytomany field: First, return all the possible manytomany objects:

profiles = Profile.objects.all()

Next, filter the model by returning only the queryset containing at least one of the profiles:

hid_parties = Party.objects.filter(profiles__in=profiles)

To do the above in a single line:

hid_parties = Party.objects.filter(profiles__in=Profile.objects.all())

You can further refine individual querysets the normal way for more specific filtering.
NOTE:This may not be the most effective way, but at least it works for me.

Upvotes: 0

Kostyantyn
Kostyantyn

Reputation: 5191

Derived from @Yuji-'Tomita'-Tomita answer, I've also added .distinct('id') to exclude the duplitate records:

Party.objects.filter(organizer=user, participants__isnull=False).distinct('id')

Therefore, each party is listed only once.

Upvotes: 5

solartic
solartic

Reputation: 4319

If this works this is how I would do it.

Best way can mean a lot of things: best performance, most maintainable, etc. Therefore I will not say this is the best way, but I like to stick to the ORM features as much as possible since it seems more maintainable.

from django.db.models import Count

user = Hipster.objects.get(pk=1) 
hip_parties = (Party.objects.annotate(num_participants=Count('participants'))
                            .filter(organiser=user, num_participants__gt=0))

Upvotes: 154

Arseniy Panfilov
Arseniy Panfilov

Reputation: 361

Easier with exclude:

# organized by user and has more than 0 participants
Party.objects.filter(organizer=user).exclude(participants=None)

Also returns distinct results

Upvotes: 6

Party.objects.filter(organizer=user, participants__isnull=False)
Party.objects.filter(organizer=user, participants=None)

Upvotes: 39

Related Questions