Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向django中的组添加用户_Python_Django - Fatal编程技术网

Python 向django中的组添加用户

Python 向django中的组添加用户,python,django,Python,Django,如何在django中将用户以组名添加到组中 我可以这样做: user.groups.add(1) # add by id 我该如何做这样的事情: user.groups.add(name='groupname') # add by name 使用具有组名称的组模型查找组,然后将用户添加到用户集 from django.contrib.auth.models import Group my_group = Group.objects.get(name='my_group_name') my_

如何在django中将用户以组名添加到组中

我可以这样做:

user.groups.add(1) # add by id
我该如何做这样的事情:

user.groups.add(name='groupname') # add by name

使用具有组名称的组模型查找组,然后将用户添加到用户集

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

以下是在Django的现代版本(在Django 1.7中测试)中如何做到这一点:


coredumperror是正确的,但我发现了一件事,我需要分享这件事

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
来自django.contrib.auth.models导入组的

#由于以下原因导致获取或创建返回错误:
新建组=组。对象。获取组或创建组(名称='groupName')
打印(类型(新组))#返回元组
新建组=组。对象。获取组或创建组(名称='groupName')
user.groups.add(new_group)#new_group as tuple并返回错误
#get()未返回错误,原因是
new_group=group.objects.get(name='groupName')
打印(键入(新组))#返回
user=user.objects.get(用户名='username')
user.groups.add(新建组)#新建组作为对象并添加用户

谢谢您的帮助。django文档中缺少或很难找到一些最基本的东西,这似乎很愚蠢。在“使用API”一节中有类似的示例。教程非常有用,我的意思是,我希望在文档的“auth”一节中看到以编程方式创建组。取而代之的是一个薄弱的段落:我想这有助于记住,auth模型只是常规模型,标准模型参考适用。Django doc中的
user\u set
在哪里?我找不到anywhere@MinhThai反向关系字段的默认值为
\u set
,但字段上未设置
相关的\u name
。此解决方案版本是否敏感?当我尝试这是django 1.8时,我得到了“意外关键字:名称”,您也可以执行
Group.objects.get\u by_natural\u key('groupname')
,但它不会使它变短:D@enchance只要你需要这样做。可能在执行组分配的视图的代码中。这是因为get_或_create返回一个元组:(object,created)
new\u group,created=group.objects.get\u或\u create(name='groupName')
这样做太好了
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