将用户添加到 django 中的组 [英] Adding a user to a group in django
本文介绍了将用户添加到 django 中的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过组名将用户添加到 django 中的组?
How would I add a user to a group in django by the group's name?
我可以做到:
user.groups.add(1) # add by id
我将如何做这样的事情:
How would I do something like this:
user.groups.add(name='groupname') # add by name
推荐答案
使用Group模型和组名找到组,然后将用户添加到user_set中
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)
这篇关于将用户添加到 django 中的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文