Add a custom permission to a User

笑着哭i 提交于 2019-11-30 11:30:49

问题


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?


回答1:


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 python manage.py makemigrations && python manage.py migrate.

Use the permission_required decorator to restrict access to your view.



来源:https://stackoverflow.com/questions/1876424/add-a-custom-permission-to-a-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!