将用户添加到django中的组 [英] Adding a user to a group in django

查看:100
本文介绍了将用户添加到django中的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这么做:

  user.groups.add(1)#添加ID 

我该如何做这样的事情:

  user.groups.add(name ='groupname')#add按名称


解决方案

使用组名称的组,然后将用户添加到user_set

  from django.contrib.auth.models import Group 
g = Group.objects.get(name ='groupname')
g.user_set.add(your_user)


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

解决方案

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
g = Group.objects.get(name='groupname') 
g.user_set.add(your_user)

这篇关于将用户添加到django中的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆