如何在 Django 中获取具有特定权限组的所有用户的列表 [英] How to get a list of all users with a specific permission group in Django
本文介绍了如何在 Django 中获取具有特定权限组的所有用户的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取具有特定权限组的所有 Django auth 用户的列表,如下所示:
I want to get a list of all Django auth user with a specific permission group, something like this:
user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
我不知道如何做到这一点.权限组如何保存在用户模型中?
I cannot find out how to do this. How are the permissions groups saved in the user model?
推荐答案
如果你想通过权限获取用户列表,看看这个变体:
If you want to get list of users by permission, look at this variant:
from django.contrib.auth.models import User, Permission
from django.db.models import Q
perm = Permission.objects.get(codename='blogger')
users = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm)).distinct()
这篇关于如何在 Django 中获取具有特定权限组的所有用户的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文