Filtering a QuerySet by the result of user.has_perm

421 views
Skip to first unread message

Adam Gamble

unread,
Mar 10, 2015, 9:47:57 PM3/10/15
to django...@googlegroups.com
Hi all,

I'm stuck on how to restructure the following in order to return a QuerySet instead of a List (needed for Lazy evaluation):

filter(lambda o: user.has_perm('read', o), MyModel.objects.all())

Using django-guardian, for object-level permissions.

Appreciate any ideas,

Adam

Collin Anderson

unread,
Mar 12, 2015, 5:31:16 PM3/12/15
to django...@googlegroups.com
Hi Adam,

It's pretty inefficient, but you can do:
MyModel.objects.filter(pk__in=[obj.pk for obj in MyModel.objects.all() if user.has_perm('read', obj)])

But, actually, I think you want to use get_objects_for_user():

Collin
Reply all
Reply to author
Forward
0 new messages