Search
Clear search
Close search
Main menu
Google apps
Groups
Sign in
Groups
Django users
Conversations
About
Send feedback
Help
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 PM
3/10/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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 PM
3/12/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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():
http://django-guardian.readthedocs.org/en/v1.2/userguide/check.html#get-objects-for-user
Collin
Reply all
Reply to author
Forward
0 new messages