3

I have assigned a user to a group say developer and in my admin I want a query like:

user_group = request.user.groups

Its giving me auth.Group.None though I have assigned a user in that group.

Why I am getting None while I have already assigned a user to the group

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
varad
  • 7,309
  • 20
  • 60
  • 112

2 Answers2

3

As MattH pointed out here https://stackoverflow.com/a/2245908/5936450 you could use the following (slightly altered for your example):

user_group = request.user.groups.values_list('name', flat=True)
Community
  • 1
  • 1
Curtis Olson
  • 947
  • 1
  • 5
  • 10
1

You have first to query all groups of a specific user using this

request.user.groups.all()

it will return a list of groups for that user

then for a getting a specific one, suppose it is the first element in the list

group = request.user.groups.all()[0].name
Norhan_ms
  • 11
  • 2