Using django exclude filter problem with empty m2m field
The queryset I've constructed is incorrectly leaving out some items. I have three models in Django:
class Case(models.Model):
agents = models.ManyToManyField('UserProfile', related_name='agent_of', blank=True, null=True)
organization = models.ForeignKey(Organization, related_name='case_org')
class UserProfile(models.Model):
name = models.CharField(ma开发者_开发问答x_length=40)
user = models.ForeignKey(User, unique=True, related_name='user_profile')
organization = models.ForeignKey(Organization, related_name='org_members', blank=True, null=True)
class Organization(models.Model):
name = models.CharField(max_length=75)
I'm trying to build a list of unassigned cases. That is, cases that the current user is not an agent on, including cases that have no agents at all assigned to them. Here's my query:
Case.objects.filter(
organization=request.user.user_profile.get().organization.id).exclude
(Q(agents__user=request.user))
This works fine for cases with other agents (UserProfile model) assigned to them. But it does not return cases with NO agent assigned to them. I'm pretty sure this has to do with the fact that cases with no agent assigned to them have no row in the intermediate table connecting UserProfiles and Cases.
So in other words if I have these cases:
Case/Agents
Case1: Tom, Steve
Case2: Steve
Case3: Jane
Case4: Nobody
My query will return Case2 and Case3, but not Case4. Trying to get that Case4 included.
Sorry if this is not very clear, any help is appreciated.
The question is a bit unclear but does this query not work to get all cases that aren't assigned to that user?
Case.objects.exclude(agents=request.user)
If you are trying to get cases belonging to user's organization AND not assigned to him OR NOT unassigned to anybody this should work.
Case.objects.filter(Q(organization=organization)|Q(agents=None)).exclude(agents=request.user)
精彩评论