在 Django 中,如何检查用户是否属于某个组?
我在 Django 的管理站点中创建了一个自定义组。
在我的代码中,我想检查用户是否在该组中。我该怎么做?
I created a custom group in Django's admin site.
In my code, I want to check if a user is in this group. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您的User对象通过ManyToMany关系链接到Group对象。
因此,您可以将filter方法应用于user.groups。
因此,要检查给定用户是否属于某个特定组(例如“成员”),只需执行以下操作:
如果要检查给定用户是否属于多个给定组,请使用 __in< /strong> 运算符,如下所示:
请注意,这些函数可以与 @user_passes_test 装饰器一起使用来管理对视图的访问:
对于基于类的视图,您可以将
UserPassesTestMixin
与test_func
方法:希望有帮助
Your User object is linked to the Group object through a ManyToMany relationship.
You can thereby apply the filter method to user.groups.
So, to check if a given User is in a certain group ("Member" for the example), just do this :
If you want to check if a given user belongs to more than one given groups, use the __in operator like so :
Note that those functions can be used with the @user_passes_test decorator to manage access to your views :
For class based views, you might use
UserPassesTestMixin
withtest_func
method:Hope this help
您只需通过
User
上的groups
属性即可访问组。然后
user.groups.all()
返回[]
。或者,更直接地,您可以通过以下方式检查用户是否在组中:
请注意,
groupname
也可以是实际的 Django Group 对象。You can access the groups simply through the
groups
attribute onUser
.then
user.groups.all()
returns[<Group: Editor>]
.Alternatively, and more directly, you can check if a a user is in a group by:
Note that
groupname
can also be the actual Django Group object.如果您不需要现场的用户实例(就像我一样),您可以使用
这将只产生一个对数据库的请求并返回一个布尔值。
If you don't need the user instance on site (as I did), you can do it with
This will produce only one request to the database and return a boolean.
如果您需要某个组中的用户列表,您可以这样做:
然后检查
该用户是否在该组中。
更新 2023
10 年后再看这个解决方案,我非常确定我不想像这样获取完整的用户列表。如果规模扩大的话,就会出现问题。您只想在非常特定的用例中获取用户列表,在该用例中可以保证用户列表保持较小,或者您只是使用 Django shell。
If you need the list of users that are in a group, you can do this instead:
and then check
to check if the user is in the group.
Update 2023
Looking at this solution 10 years later, I'm pretty sure that I would NOT want to ever to fetch a whole list of users like this. It's something that would be problematic at scale. You would only want to fetch a list of users in a very specific use case where there were assurances that the list of users was going to remain small, or if you were just using the Django shell.
用户是否属于某个组,可以使用以下命令在 django 模板中检查:
{% if group in request.user.groups.all %}
“一些动作”
{% endif %}
If a user belongs to a certain group or not, can be checked in django templates using:
{% if group in request.user.groups.all %}
"some action"
{% endif %}
你只需要一行:
You just need one line:
使用这个:
Use this:
我有类似的情况,我想测试用户是否属于某个组。因此,我创建了新文件 utils.py,其中放置了所有可以帮助我完成整个应用程序的小实用程序。在那里,我有了这样的定义:
所以基本上我正在测试用户是否在 company_admin 组中,为了清楚起见,我将此函数称为 is_company_admin。
当我想检查用户是否在 company_admin 中时,我只需这样做:
现在,如果您想在模板中测试相同的内容,您可以在上下文中添加 is_user_admin ,像这样:
现在您可以在模板中评估您的响应:
简单而干净的解决方案,基于可以在本线程前面找到的答案,但以不同的方式进行。希望它能帮助某人。
在 Django 3.0.4 中测试。
I have similar situation, I wanted to test if the user is in a certain group. So, I've created new file utils.py where I put all my small utilities that help me through entire application. There, I've have this definition:
so basically I am testing if the user is in the group company_admin and for clarity I've called this function is_company_admin.
When I want to check if the user is in the company_admin I just do this:
Now, if you wish to test same in your template, you can add is_user_admin in your context, something like this:
Now you can evaluate you response in a template:
Simple and clean solution, based on answers that can be found earlier in this thread, but done differently. Hope it will help someone.
Tested in Django 3.0.4.
以防万一您想检查用户的组是否属于预定义的组列表:
Just in case if you wanna check user's group belongs to a predefined group list:
一行:
计算结果为
True
或False
。In one line:
This evaluates to either
True
orFalse
.我已经按照以下方式完成了。看起来效率很低,但我心里没有其他办法:
I have done it the following way. Seems inefficient but I had no other way in my mind:
User.objects.filter(username='tom', groups__name='admin').exists()
该查询将通知您用户:“tom”是否属于组“admin”
User.objects.filter(username='tom', groups__name='admin').exists()
That query will inform you user : "tom" whether belong to group "admin " or not
我就是这样做的。对于名为
Editor
的组。模板
I did it like this. For group named
Editor
.template