开发者

Add a custom permission to a User

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开发者_JAVA技巧 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 python manage.py makemigrations && python manage.py migrate.

Use the permission_required decorator to restrict access to your view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜