[Answered ]-Django join query for permissions and group?

2👍

I think the below query would work for you

from django.db.models import Case, When, BooleanField

group_name = 'your group name'
Permission.objects.annotate(
    has_perm=Max(Case(
        When(group__name=group_name, then=1),
        default=0,
        output_field=BooleanField()
    ))
).values_list('name', 'has_perm').order_by('has_perm')

Conditional expressions were introduced in Django 1.8, and aggregation (annotate) is also well documented.

👤zsepi

Leave a comment