在django中有一个用户的组名称很简单 [英] is there a simple to get group names of a user in django

查看:100
本文介绍了在django中有一个用户的组名称很简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 django.contrib.auth.User 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

感谢任何帮助!

推荐答案

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

或最近的django

or with recent django

l = request.user.groups.values_list('name',flat=True)

这篇关于在django中有一个用户的组名称很简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆