Reputation: 1599
I'm using the row-level permission model known as django-granular-permissions (http://code.google.com/p/django-granular-permissions/). The permission model simply has just two more fields which are content-type and object id.
I've used the following query:
User.objects.filter(Q(row_permission_set__name='staff') | \
Q(row_permission_set__name='student'), \
row_permission_set__object_id=labsite.id)
I want to add is_staff
and is_student
boolean fields to the result set without querying everytime when I fetch the result.
Django documentation shows extra() method of querysets, but I can't figure out what I should write for plain SQL selection query with this relation.
How to do this?
Upvotes: 2
Views: 3826
Reputation: 2393
.extra(select={'is_staff': "%s.name='staff'" % Permission._meta.db_table, 'is_student': "%s.name='student'" % Permission._meta.db_table, })
Upvotes: 5
Reputation: 1859
Normally you'd use select_related() for things like this, but unfortunately it doesn't work on reverse relationships. What you could do is turn the query around:
users = [permission.user for permission in Permission.objects.select_related('user').filter(...)]
Upvotes: 0