183

How would I add a user to a group in django by the group's name?

I can do this:

user.groups.add(1) # add by id

How would I do something like this:

user.groups.add(name='groupname') # add by name
1
  • Is this solution version sensitive? When I tried this is django 1.8, I got "unexpected keyword: name"
    – rschwieb
    Aug 8, 2016 at 20:17

4 Answers 4

343

Find the group using Group model with the name of the group, then add the user to the user_set

from django.contrib.auth.models import Group
my_group = Group.objects.get(name='my_group_name') 
my_group.user_set.add(your_user)
6
  • 32
    Thanks for this. It seems silly that some of the most basic things are either missing or hard to find in the django docs Sep 1, 2011 at 15:38
  • 1
    docs.djangoproject.com/en/dev/intro/tutorial01 There are similar examples in section 'Playing with the API' Sep 1, 2011 at 17:02
  • 12
    The tutorial is pretty useful, What I meant was that I would expect to see in a section of the docs under auth for programmatically creating groups. instead all there is is a weak paragraph: docs.djangoproject.com/en/1.3/topics/auth/#groups I guess it helps to keep in mind that the auth models are just regular models, and the standard model reference applies. Sep 1, 2011 at 18:41
  • where is user_set in Django doc? I cannot find it anywhere
    – Minh Thai
    Apr 10, 2019 at 7:07
  • 1
    @MinhThai the default value for a reverse relation field is <content_type>_set when related_name is not set on the field. Oct 22, 2019 at 9:45
133

Here's how to do this in modern versions of Django (tested in Django 1.7):

from django.contrib.auth.models import Group
group = Group.objects.get(name='groupname')
user.groups.add(group)
2
  • 1
    you can also do Group.objects.get_by_natural_key('groupname'), but it doesn't make it shorted :D
    – CpILL
    Feb 2, 2017 at 1:18
  • 2
    @enchance Wherever you need to do so. Probably within the code for a View that's doing Group assignments. Jun 9, 2017 at 16:35
11

coredumperror is right but I have found one thing I need to share that one

from django.contrib.auth.models import Group

# get_or_create return error due to 
new_group = Group.objects.get_or_create(name = 'groupName')
print(type(new_group))       # return tuple

new_group = Group.objects.get_or_create(name = 'groupName')
user.groups.add(new_group)   # new_group as tuple and it return error

# get() didn't return error due to 
new_group = Group.objects.get(name = 'groupName')
print(type(new_group))       # return <class 'django.contrib.auth.models.Group'>

user = User.objects.get(username = 'username')
user.groups.add(new_group)   # new_group as object and user is added
3
  • 6
    this is because get_or_create returns a tuple: (object, created). new_group, created = Group.objects.get_or_create(name = 'groupName') would work
    – t1m0
    Mar 11, 2021 at 17:29
  • oh that's too great May 10, 2021 at 4:45
  • Remember to import also User since you call the object. Basically: from django.contrib.auth.models import Group, User Mar 2 at 10:15
3

You can assign multiple groups to a user using the set method:

from django.contrib.auth.models import Group

users = Group.objects.get(name="user")
managers = Group.objects.get(name="manager")
user.groups.set([users, managers])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.