Created
May 31, 2019 15:11
-
-
Save Asinox/c20a5d06c29af54c09052e893b2b05e3 to your computer and use it in GitHub Desktop.
Filter Django user permissions from admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Si el usuario pertenece a uno o varios grupos y estos grupos tienen permisos, no muestres estos permisos en el campo para asignar permisos individuales en django admin | |
""" | |
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin | |
from django.contrib.auth.models import Group, Permission | |
class UserAdmin(BaseUserAdmin): | |
def get_form(self, request, obj=None, **kwargs): | |
form = super(UserAdmin, self).get_form(request, obj, **kwargs) | |
if 'user_permissions' in form.base_fields and obj: | |
groups = obj.groups.values_list('id',flat=True) | |
gname = [] | |
for g_id in groups: | |
gp = Permission.objects.filter(group__id__in=[g_id]) | |
for p in gp: | |
gname.append(p.codename) | |
permissions = form.base_fields['user_permissions'] | |
permissions.queryset = permissions.queryset.exclude(codename__in=gname) | |
return form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment