Dibidalidomba
Dibidalidomba

Reputation: 777

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)
else:
    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.

Upvotes: 3

Views: 78

Answers (1)

Seif
Seif

Reputation: 1097

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?

Upvotes: 1

Related Questions