Django: query spanning multiple many-to-many relationships

Posted by Brant on Stack Overflow See other posts from Stack Overflow or by Brant
Published on 2010-05-21T15:35:44Z Indexed on 2010/05/21 15:40 UTC
Read the original article Hit count: 224

Filed under:
|

I've got some models set up like this:

class AppGroup(models.Model):
  users = models.ManyToManyField(User)

class Notification(models.Model):
  groups_to_notify = models.ManyToManyField(AppGroup)

The User objects come from django's authentication system.

Now, I am trying to get all the notifications pertaining to the groups that the current user is a part of. I have tried..

notifications = Notification.objects.filter(groups_to_notify=AppGroup.objects.filter(users=request.user))

But that gives an error:

more than one row returned by a subquery used as an expression

Which I suppose is because the groups_to_notify is checking against several groups.

How can I grab all the notifications meant for the user based on the groups he is a part of?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models