Django provides convenient methods for adding the same permissions to all models
The contrib. auth library provided by Django has powerful management functions. It creates three default permissions for each module: add, change, and delete. However, what should we do if we want to add the same permissions (such as read) to all models?
If permissions is added to the class Meta of each Model, it is necessary to modify all models to add the same row, and the method is too earthy.
A shortcut is: modify the code line of Django. contrib. auth. The file path is django \ contrib \ auth \ management \ __init _. py. The code snippet is as follows:
Def _ get_all_permissions (opts ):
"Returns (codename, name) for all permissions in the given opts ."
Perms = []
For action in ('add', 'change', 'delete '):
Perms. append (_ get_permission_codename (action, opts), u 'can % s % s' % (action, opts. verbose_name_raw )))
Return perms + list (opts. permissions)
Add the corresponding permissions in this line, for example:
For action in ('add', 'change', 'delete '):
Changed:
For action in ('read', 'add', 'change', 'delete '):
Run python manage. py syncdb and you will see that the read permission for all models has been added to the auth_permission table. Is it convenient?
For more custom permissions for a Model, see the official documentation: https://docs.djangoproject.com/en/1.4/topics/auth/#custom-permissions
Install Nginx + uWSGI + Django on Ubuntu Server 12.04
Django tutorial
Build a Django Python MySQL Linux development environment
Django details: click here
Django's: click here
This article permanently updates the link address: