我试图在django中找到与特定用户关联的组名

时间:2015-07-03 06:03:40

标签: django

我正面临这些错误

l = request.user.groups.values_list('name',flat=True)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'request' is not defined

1 个答案:

答案 0 :(得分:1)

  

NameError:name&#39; request&#39;未定义

似乎您正在使用未定义的变量request, 例如,request可以来自一个视图,例如

from django.views.generic import View

class MyView(View):
    def get(self, request):
        # here you can use the request object
        user = request.user
        user.groups... # do something with the user groups
  

我正在尝试在django中找到与特定用户相关联的群组名称

您也可以直接使用该用户而无需任何HttpRequest对象

from django.contrib.auth.models import User

user = User.objects.get(username='ashish')
user.groups... # do something with the user groups