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

Question

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

Answers 1
Added at 2017-01-04 15:01
Tags
Question

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.

Answers
nr: #1 dodano: 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?

Source Show
◀ Wstecz