Django 1.5,python 2.6
Django 1.5, python 2.6
模型在特定条件下自动创建用户:
The model automatically creates a user under certain conditions:
User.objects.get_or_create(username=new_user_name, is_staff=True)
u = User.objects.get(username=new_user_name)
u.set_password('temporary')
除了设置用户名、密码和 is_staff 状态之外,我还想设置用户的权限 - 类似:
In addition to setting the username, password, and is_staff status, I would like to set the user's permissions - something like:
u.user_permissions('Can view poll')
或
u.set_permissions('Can change poll')
这可能吗?谢谢!
使用 add
和 remove
方法:
Use add
and remove
methods:
from django.contrib.auth.models import Permission
permission = Permission.objects.get(name='Can view poll')
u.user_permissions.add(permission)
这篇关于django - 使用 get_or_create 自动创建用户时设置用户权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!