向用户添加自定义权限 [英] Add a custom permission to a User
问题描述
我想给一些现有用户一个自定义权限,我需要访问视图。
我想我需要添加新的允许Postgres表auth_permission,但我怀疑有更高级的方法来做到这一点。还有一个列在auth_permission for content_type,我不知道它的值应该是什么。
这是什么正确的方法?
看看如何创建自定义权限在文档中。
class USCitizen
#
class Meta:
permission =(
(can_drive,可以驱动),
(can_vote ,可以投票选举),
(can_drink,可以喝酒),
)
然后运行 manage.py syncdb
。
使用 permission_required decorator 以限制对您的视图的访问。 / p>
I'd like to be able to give some existing Users a custom permission which I will require for accessing a view.
I think I need to add the new permission to the Postgres table auth_permission, but I suspect there is a higher-level way to do this. Also there is a column in auth_permission for content_type and I don't know what its value should be.
What is the right way to do this?
Have a look at how to create custom permissions in the docs.
class USCitizen(models.Model):
# ...
class Meta:
permissions = (
("can_drive", "Can drive"),
("can_vote", "Can vote in elections"),
("can_drink", "Can drink alcohol"),
)
Then run manage.py syncdb
.
Use the permission_required decorator to restrict access to your view.
这篇关于向用户添加自定义权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!