有没有一种简单的方法可以在 django 中获取用户的组名

发布于 2024-08-20 22:28:50 字数 564 浏览 7 评论 0原文

我尝试在 django.contrib.auth.Userdjango.contrib.auth.Group 的帮助下遵循代码,

for g in request.user.groups:
    l.append(g.name)

但是失败了,我收到了以下错误< /strong>:

TypeError at /
'ManyRelatedManager' object is not iterable
Request Method: GET
Request URL:    http://localhost:8000/
Exception Type: TypeError
Exception Value:    
'ManyRelatedManager' object is not iterable
Exception Location: C:\p4\projects\...\users.py in permission, line 55

感谢您的帮助!

I tried following Code with the help of the django.contrib.auth.User and django.contrib.auth.Group

for g in request.user.groups:
    l.append(g.name)

But that failed and I received following Error:

TypeError at /
'ManyRelatedManager' object is not iterable
Request Method: GET
Request URL:    http://localhost:8000/
Exception Type: TypeError
Exception Value:    
'ManyRelatedManager' object is not iterable
Exception Location: C:\p4\projects\...\users.py in permission, line 55

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一生独一 2024-08-27 22:28:50

您可以使用 request.user.groups.all() 获取用户的组,这将返回一个 QuerySet。然后,如果需要,您可以将该对象转换为列表。

for g in request.user.groups.all():
    l.append(g.name)

或者最近的 Django

l = request.user.groups.values_list('name',flat = True) # QuerySet Object
l_as_list = list(l)                                     # QuerySet to `list`

You can get the groups of a user with request.user.groups.all(), which will return a QuerySet. And then you can turn that object into a list if you want.

for g in request.user.groups.all():
    l.append(g.name)

or with recent Django

l = request.user.groups.values_list('name',flat = True) # QuerySet Object
l_as_list = list(l)                                     # QuerySet to `list`
半边脸i 2024-08-27 22:28:50

这更好

if user.groups.filter(name='groupname').exists():
    # Action if existing

else:
    # Action if not existing

This is better

if user.groups.filter(name='groupname').exists():
    # Action if existing

else:
    # Action if not existing
北凤男飞 2024-08-27 22:28:50
user.groups.all()[0].name == "groupname"
user.groups.all()[0].name == "groupname"
痴意少年 2024-08-27 22:28:50

这可能有点太晚了(我刚刚加入 stackoverflow),但是对于 2018 年初通过谷歌搜索的人来说,您可以使用 django Groups 对象(默认情况下)附带以下字段(不是详尽的,只是重要的字段)这一事实):

id、名称、权限、用户(可以有多个用户;ManyToMany)

请注意,一个组可以由多个用户组成,一个用户可以是多个组的成员。因此,您可以简单地过滤当前用户会话的 django Groups 模型(确保您已添加相关组并将用户分配到他/她的组):

'''
This assumes you have set up django auth properly to manage user logins
'''
# import Group models
from django.contrib.auth.models import Group

# filter the Group model for current logged in user instance
query_set = Group.objects.filter(user = request.user)

# print to console for debug/checking
for g in query_set:
    # this should print all group names for the user
    print(g.name) # or id or whatever Group field that you want to display

This is probably tad bit too late (I just joined stackoverflow), but for anyone googling for this in early 2018, you can use the fact that django Groups object (by default) comes with the following fields (not exhaustive , just the important ones):

id, name, permissions, user (can have many users; ManyToMany)

Note that a group can consist of many users, and a user can be a member of many groups. So you can simply filter the django Groups model for the current user-session (make sure you have added the relevant groups and assigned the user to his/her group/s):

'''
This assumes you have set up django auth properly to manage user logins
'''
# import Group models
from django.contrib.auth.models import Group

# filter the Group model for current logged in user instance
query_set = Group.objects.filter(user = request.user)

# print to console for debug/checking
for g in query_set:
    # this should print all group names for the user
    print(g.name) # or id or whatever Group field that you want to display
思慕 2024-08-27 22:28:50

您也可以使用此选项
在你看来

  users =User.objects.all().exclude(is_superuser=True)
    context = {'users':users}
    return render(request,'yourpage.html',context)

在 yourpage.html 中

{% for users in users %}
  <tr>
    <td>{{users.username}}</td>
    <td>{{users.email}}</td>
    <td>
        {%for group in users.groups.all %}
        {{ group.name }}
        {%endfor%}
    </td>

this will display all users with their group

You can also use this option
In you views

  users =User.objects.all().exclude(is_superuser=True)
    context = {'users':users}
    return render(request,'yourpage.html',context)

In yourpage.html

{% for users in users %}
  <tr>
    <td>{{users.username}}</td>
    <td>{{users.email}}</td>
    <td>
        {%for group in users.groups.all %}
        {{ group.name }}
        {%endfor%}
    </td>

this will display all users with their group

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文