Add a custom permission to a User
Asked Answered
O

1

41

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?

Otherworld answered 9/12, 2009 at 19:58 Comment(0)
U
63

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.

Umpteen answered 9/12, 2009 at 20:1 Comment(4)
Thanks. That worked. What initially confused me was that it creates a new model just for the permission. But I guess that makes sense.Otherworld
this didn't work for me. I changed the tuple to a list and it worked. The permissions looked like. permissions = [ ("",""),("","")]Trinitarianism
How do you dynamically make this in the VUE UI?Pieria
Ritesh above is correct, as also confirmed by the docs, at least for current Django version 3.2 docs.djangoproject.com/en/3.2/topics/auth/customizing/…Medina

© 2022 - 2024 — McMap. All rights reserved.