#27260: Performance Issue because of LEFT OUTER JOIN instead the better INNER
JOIN
-------------------------------------+-------------------------------------
Reporter: Sven R. Kunze | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Thomas Güttler):
Just for the records, we use this pattern until we update to Django 1.11:
Use case: Select users which have permission "some_app.permission_name"
and which are in group "special_group"
{{{
user_ids_with_group =
User.objects.filter(groups__in=[special_group]).distinct().values_list('pk',
flat=True)
user_ids_with_permission =
User.objects.filter(authutils.users_with_perm_q('some_app.permission_name')).distinct().values_list('pk',
flat=True)
qs =
User.objects.filter(pk__in=set(user_ids_with_group).intersection(user_ids_with_permission)).distinct().order_by('last_name',
'first_name')
# authutils
def users_with_perm_q(perm, is_active=True, is_superuser=True):
try:
app_label, codename = perm.split('.')
except ValueError:
raise ValueError('Permission name should be in the form
"app_label.perm_name".')
permission = Permission.objects.get(codename=codename,
content_type__app_label=app_label)
user_q = Q(user_permissions=permission)
group_q = Q(groups__permissions=permission)
return Q(is_active=is_active) & (Q(is_superuser=is_superuser) | user_q
| group_q)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27260#comment:10>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.f692ee062ae8f3382fcfbcb95cf90b44%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.