varad
varad

Reputation: 8039

django user group from request user

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

Upvotes: 3

Views: 6627

Answers (2)

Norhan_ms
Norhan_ms

Reputation: 11

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

Upvotes: 1

Curtis Olson
Curtis Olson

Reputation: 967

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)

Upvotes: 3

Related Questions