How to filter queryset by ForeignKey and ManyToMany using Q properly?


I got an error message that says:

'User' object does not support indexing

I try to filter queryset using Q to get a query that contains objects where self.request.user (which is an User model object) is an owner (ForeignKey) or one of the participants (ManyToMany).

Here is my code:

if self.request.user.has_perm('permissions'):
    queryset = self.model.objects.filter(parent=None)
    queryset = self.model.objects.filter(Q(parent=None),
                                         Q(owner=self.request.user) | Q(participants__in=self.request.user))

I think the problem is Q(participants__in=self.request.user) but I have no idea how to fix it.

Show source
| python   | django   2017-01-04 15:01 1 Answers

Answers ( 1 )

  1. 2017-01-04 16:01

    Your error comes from this Q(participants__in=self.request.user) when you use in you have to specify an iterable.

    Can you post models?

◀ Go back