简体   繁体   中英

Would this hack for per-object permissions in django work?

According to the documentation, a class can have the meta option permissions, described as such:

Options.permissions

Extra permissions to enter into the permissions table when creating this object. Add, delete and change permissions are automatically created for each object that has admin set. This example specifies an extra permission, can_deliver_pizzas:

 permissions = (("can_deliver_pizzas", "Can deliver pizzas"),) 

This is a list or tuple of 2-tuples in the format (permission_code, human_readable_permission_name).

Would it be possible to define permissions at run time by:

 permissions = (("can_access_%s" % self.pk, / "Has access to object %s of type %s" % (self.pk,self.__name__)),) 

?

I think in the context of the Meta class, you don't have access to self .
If you look for a solution for the admin application, read this about row level permissions .

There is also says:

For public-facing (ie, non-admin) views, you are of course free to implement whatever form of permission-checking logic your application requires.

No, this wouldn't work, for a number of reasons. Firstly, as Felix points out, you have no access to self at that point. Secondly, as the documentation you quoted states, this is a list of items to enter into the permissions table - in other words these are actual database rows, which are created by manage.py syncdb .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM