5👍
You can read in details about django permissions in the docs
https://docs.djangoproject.com/en/dev/topics/auth/default/#permissions-and-authorization
Basically Django permissions use the Permission
model, which is found at django.contrib.auth.models
, but for most applications you don’t need to directly import or use that model.
By default Django creates 3 default permissions for any model you have in your app. If you have a model named MyModel
in an app named myapp
, then Django will create create_mymodel
, change_mymodel
, and delete_mymodel
permissions by default.
You can check if the user has a certain permission by calling
user.has_perm('myapp.create_mymodel')
if you’re checking for the create
permission for example. Or, like you did, you can use the decorator
permission_required('myapp.create_mymodel')
In addition to the default permissions provided by django, you can define custom permissions on your models by specifying the permissions
attribute in the Meta
class of your model like this:
class MyModel(models.Model):
[...]
class Meta:
permissions = (
("can_deliver_pizzas", "Can deliver pizzas"),
)
More on defining custom permissions here: https://docs.djangoproject.com/en/dev/ref/models/options/#permissions
By default, permissions can be easily edited for every user using the admin interface. Just visit a certain user’s page and there will be a field named User Permissions with a list of all permissions in your project, from which you can add or remove permissions for your particular user.