[Django] User permission Learning Series-User permission basic operation commands, django operation commands

Source: Internet
Author: User

[Django] User permission Learning Series-User permission basic operation commands, django operation commands

For the user management system that comes with Django background, although it still feels OK, but in order to facilitate some user operations, a custom user permission management system is specially designed.

Before creating the permission page, you must first understand the permissions and user configuration permission commands. The previous chapter describes how to add, delete, and query permissions. This chapter describes operation instructions for user permissions.

First, import the Permission, User module:

From django. contrib. auth. models import Permission, User # User Permissions

Add permission:

Permission. objects. create (name = u'permission management', content_type_id = 2, codename = u'permission managementdescription ')

 

Add User Permissions:

Perm = Permission. objects. get (codename = u'permission management') # First you need to add the "permission management" permission User. objects. get (username = '123 '). user_permissions.remove (perm)

Delete User Permissions:

Perm = Permission. objects. get (codename = u'permission management') # First you need to add the "permission management" permission User. objects. get (username = '123 '). user_permissions.remove (perm)

Batch Add User Permissions:

Perm1 = Permission. objects. get (codename = u'permission management') perm2 = Permission. objects. get (codename = u'user management') User. objects. get (username = '123 '). user_permissions.add (perm1, perm2)

Clear User Permissions:

User.objects.get(username='270001').user_permissions.clear()

Query user permissions and Output

 

c = User.objects.get(username='270001').user_permissions.values()for i in c:    print i

 

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.