0

Am new to Django, have been working on linking some permissions to groups and then adding users to those groups. Have managed some of that so far, but am stuck on understanding exactly what is going on with the permissions. Here is my attempt so far:

(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()

then

>>> 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>]>>

then

>>> 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'}]>

Of all the permissions here, most of them were part of Django but the one with name 'Can write stuff.' I added previously.

So the output of p.values() is relatively intuitive, each permission has a set of keys and values linked to it.

Am really stuck though on understanding the output of 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>

Only the third part of each of these elements of the QuerySet after the pipe is found in the dictionaries from p.values() under 'name', eg 'Can add user'

What is the meaning of the first and second part of each of these?

cardamom
  • 6,873
  • 11
  • 48
  • 102
  • I'm not sure why it is not obvious to you that those are the app and the model name, but if you really couldn't work it out you could always [look at the code](https://github.com/django/django/blob/master/django/contrib/auth/models.py#L72). – Daniel Roseman Nov 21 '18 at 16:06
  • Thanks, that would mean some model names have a space in them, eg "log entry". I thought the only 'app' Django had was the one you made when you gave it the command `django-admin startapp MyApp` but now am learning, just like the models, a number of 'apps' are probably already built in. – cardamom Nov 21 '18 at 16:17
  • They're not built-in, you actually specified them yourself with the setting `INSTALLED_APPS` in your settings.py file. – dirkgroten Nov 21 '18 at 16:38
  • And the spaces is just because that's the `verbose_name` of the model that is printed here. – dirkgroten Nov 21 '18 at 16:43
  • Thanks, can see that in there `INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'MyApp' ]` They somehow got in there from `django-admin startproject myproject` and `django-admin startapp MyApp` I think I added 'MyApp' manually. Thanks, is getting clearer what is going on. – cardamom Nov 21 '18 at 17:01

0 Answers0