Adding a user to a group in django

前端 未结 3 1851
既然无缘
既然无缘 2020-11-28 02:55

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 some

3条回答
  •  孤城傲影
    2020-11-28 03:36

    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)
    

提交回复
热议问题