[Django] user permission operation commands (1), django operation commands

Source: Internet
Author: User

[Django] user permission operation commands (1), django operation commands

To create a py file for testing, add the following code to the file.

# Coding: UTF-8 import OS. environ. setdefault ("DJANGO_SETTINGS_MODULE", "www. settings ") ''' when the Django version is greater than or equal to 1.7, add the following two statements: import djangodjango. setup () otherwise, the error django will be thrown. core. exceptions. appRegistryNotReady: Models aren't loaded yet. '''import djangofrom django. contrib. auth. models import Permission, User # User Permission if django. VERSION> = (1, 7): # automatically determines the VERSION of django. setup ()

Query all permissions of a user and convert the result to list

User. objects. get (username = ''). user_permissions.values () # method 1 list (User. objects. get (username =''). get_all_permissions () # method 2

Query all permissions and output them cyclically

c = Permission.objects.values()for i in c:    print i

Add the specified permission. Note that content_type_id is of the int type and corresponds to the number of the model.

Permission. objects. create (name = 'add _ logentry ', content_type_id = 1, codename = 'can add log entry ')
# Example: Use the query permission statement to view {u'content _ type_id ': 1L, 'codename': u'add _ logentry', u'id': 1L, 'name': u'can add log entry '}] similar to this list,

Deletes a specified permission.

Permission.objects.get(codename='Can add log entry').delete()


Original article. For more information, see the source. Thank you!

Related Article

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.