Hi all I'm trying to combine two querysets, and having a difficult time explaining why one bit of code works, and why similar code doesn't. I'm probably doing something wrong..
This is a customer management system: User objects are arranged into UserGroup objects, with a through table called UserUserGroupPreferences, which denote things about the User's membership of the UserGroup. UserGroup objects belong to a single Organization, and each Organization has exactly one UserGroup with is_catch_all_group=True. In simplified models, looks like this (User is django.contrib.auth.User) class UserUserGroupPreferences(models.Model): usergroup = models.ForeignKey('UserGroup') user = models.ForeignKey(User) is_account_manager = models.BooleanField(default=False) class UserGroup(models.Model): organization = models.ForeignKey('Organization') users = models.ManyToManyField(User, through='UserUserGroupPreferences') is_catch_all_group = models.BooleanField(default=False) I wish to find the groups for which a particular user is an account manager, or the catch all group in the same organization as a group of which the user is an account manager. I've come up with this bit of code, which works. However, I can't understand why the last line must be in that order. filt1 = Q(userusergrouppreferences__is_account_manager=True)\ & Q(userusergrouppreferences__user=request.user) filt2 = Q(is_catch_all_group=True)\ & Q(organization__usergroup__userusergrouppreferences__user=request.user)\ & Q(organization__usergroup__userusergrouppreferences__is_account_manager=True) qs1 = UserGroup.objects.filter(filt1).distinct() qs2 = UserGroup.objects.filter(filt2).distinct() # For some reason, this is not the same as qs1 | qs2 - answers on a postcard please. managed_groups = qs2 | qs1 Any hints? Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.