Django provides convenient methods for adding the same permissions to all models

Source: Internet
Author: User
Tags python mysql django tutorial

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.