Is there a way to do row level permissions in django? I thought there wasn't but just noticed this in the docs:
Permissions can be set not only per type of object, but also per specific object instance. By using the has_add_permission(), has_change_permission() and has_delete_permission() methods provided by the ModelAdmin class, it is possible to customize permissions for different object instances of the same type.
https://docs.djangoproject.com/en/dev/topics/auth/
But i don't see any documentation on how to actually implement per instance permissions
I have rolled-out a solution to this kind of problem using Django Class Based Views.
Check out my article Django Generic Class Based Views with Object-Level Permissions Checking.
The methods that the docs talk about will allow you to restrict access to particular objects in the admin. Each method is passed the object in play, which you can use to make determinations about whether a user can access it, by returning either
True
orFalse
.For an application i'm building i want to provide row level permission through a simple decorator. I can do this because the condition is just whether the request.user is the owner of the model object.
Following seems to work:
The view:
Urls:
The model:
Any feedback or remarks are appreciated.
Paul Bormans
There are a large number of "permissions" apps for django available on PyPi
For example you could look at django-object-permission.
What the documentation is referring to is that the functionality is there to implement the permissions. And people have done just that by creating apps for this.
The plumbing is there (this is from the bottom of the same page you linked):
But no default implementation is provided. Since this is a common topic; there are lots of answers on SO. Check to the right and you'll see some listed.
The basis idea is to browse the django packages' perm grid and pick an implementation of object level permissions. I personally like django-guardian.