Add a custom permission to a User

前端 未结 1 1259
萌比男神i
萌比男神i 2020-12-13 14:24

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 Postg

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 15:05

    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 讨论(0)
提交回复
热议问题