是Django的新手,一直在努力将某些权限链接到组,然后将用户添加到这些组。到目前为止,已经管理了其中的一些内容,但仍坚持准确了解权限的状态。到目前为止,这是我的尝试:
(Django-1.11.4-env) $ python manage.py shell
Python 3.6.7 (default, Oct 25 2018, 13:09:20)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import Permission
>>> p = Permission.objects.all()
然后
>>> p.values
<bound method QuerySet.values of <QuerySet [<Permission: MyApp | user | Can add user>, <Permission: MyApp | user | Can write stuff.>, <Permission: MyApp | user | Can change user>, <Permission: MyApp | user | Can delete user>, <Permission: admin | log entry | Can add log entry>, <Permission: admin | log entry | Can change log entry>, <Permission: admin | log entry | Can delete log entry>, <Permission: auth | group | Can add group>, <Permission: auth | group | Can change group>, <Permission: auth | group | Can delete group>, <Permission: auth | permission | Can add permission>, <Permission: auth | permission | Can change permission>, <Permission: auth | permission | Can delete permission>, <Permission: contenttypes | content type | Can add content type>, <Permission: contenttypes | content type | Can change content type>, <Permission: contenttypes | content type | Can delete content type>, <Permission: sessions | session | Can add session>, <Permission: sessions | session | Can change session>, <Permission: sessions | session | Can delete session>]>>
然后
>>> p.values()
<QuerySet [{'id': 16, 'name': 'Can add user', 'content_type_id': 6, 'codename': 'add_user'}, {'id': 19, 'name': 'Can write stuff.', 'content_type_id': 6, 'codename': 'can_write'}, {'id': 17, 'name': 'Can change user', 'content_type_id': 6, 'codename': 'change_user'}, {'id': 18, 'name': 'Can delete user', 'content_type_id': 6, 'codename': 'delete_user'}, {'id': 1, 'name': 'Can add log entry', 'content_type_id': 1, 'codename': 'add_logentry'}, {'id': 2, 'name': 'Can change log entry', 'content_type_id': 1, 'codename': 'change_logentry'}, {'id': 3, 'name': 'Can delete log entry', 'content_type_id': 1, 'codename': 'delete_logentry'}, {'id': 7, 'name': 'Can add group', 'content_type_id': 3, 'codename': 'add_group'}, {'id': 8, 'name': 'Can change group', 'content_type_id': 3, 'codename': 'change_group'}, {'id': 9, 'name': 'Can delete group', 'content_type_id': 3, 'codename': 'delete_group'}, {'id': 4, 'name': 'Can add permission', 'content_type_id': 2, 'codename': 'add_permission'}, {'id': 5, 'name': 'Can change permission', 'content_type_id': 2, 'codename': 'change_permission'}, {'id': 6, 'name': 'Can delete permission', 'content_type_id': 2, 'codename': 'delete_permission'}, {'id': 10, 'name': 'Can add content type', 'content_type_id': 4, 'codename': 'add_contenttype'}, {'id': 11, 'name': 'Can change content type', 'content_type_id': 4, 'codename': 'change_contenttype'}, {'id': 12, 'name': 'Can delete content type', 'content_type_id': 4, 'codename': 'delete_contenttype'}, {'id': 13, 'name': 'Can add session', 'content_type_id': 5, 'codename': 'add_session'}, {'id': 14, 'name': 'Can change session', 'content_type_id': 5, 'codename': 'change_session'}, {'id': 15, 'name': 'Can delete session', 'content_type_id': 5, 'codename': 'delete_session'}]>
在这里的所有权限中,大多数都是Django的一部分,但我之前添加了名称为'Can write stuff.'
的那个。
因此p.values()的输出相对直观,每个权限都有一组与之链接的键和值。
尽管我对理解p.values的输出确实感到困惑
<Permission: MyApp | user | Can add user>
<Permission: admin | log entry | Can add log entry>
<Permission: contenttypes | content type | Can add content type>
<Permission: sessions | session | Can change session>
<Permission: auth | permission | Can add permission>
在p.values()的字典中,在“名称”下的“名称”(例如“可以添加用户”)中,只有QuerySet的每个元素的第三部分
每个部分的第一和第二部分是什么意思?