0

I try follow this tutorial "Programmatically creating a group : Can't access permissions from migration", and my code is:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-10-16 13:48
from __future__ import unicode_literals

from django.db import migrations, models
from django.contrib.auth.models import Group, Permission
from django.contrib.auth.management import create_permissions


def add_group_permissions(apps, schema_editor):
    for app_config in apps.get_app_configs():
        create_permissions(app_config, apps=apps, verbosity=0)

    # Criando Administrador
    group, created = Group.objects.get_or_create(name='Administrador')
    if created:
        add_thing = Permission.objects.get(
                         codename=['can_add_permision',
                                             'can_change_permission',
                                             'can_add_user',
                                             'can_change_user',
                                             'can_add_video',
                                             'can_change_video',
                                             'can_delete_video',
                                             'can_add_documents',
                                             'can_change_documents',
                                             'can_delete_documents',
                                             'can_add_news',
                                             'can_change_news',
                                             'can_delete_news',
                                             'can_add_basics',
                                             'can_change_basics',
                                             'can_add_board',
                                             'can_change_board',
                                             'can_delete_board',
                                             'can_add_history',
                                             'can_change_history',
                                             'can_delete_history',
                                             'can_add_shortcuts',
                                             'can_change_shortcuts',
                                             'can_delete_shortcuts',]
        )
        group.permissions.add(add_thing)
        group.save()
        logger.info('Grupo Administrador Criado')

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.RunPython(add_group_permissions),
    ]

And my error is:

self.model._meta.object_name django.contrib.auth.models.DoesNotExist: Permission matching query does not exist.

Is it probably that problem is releated with dependencies? Or how I do?

[UPDATE 1]

After tips of @Trilliput the group was create empty.

enter image description here

[UPDATE 2]

I removed prefix can_, after used a tips of @Trilliput, using shell python manage.py shell

from django.contrib.auth.models import Permission
permissions_qs = Permission.objects.filter(codename__in=['add_permission', 'change_permission', 'add_user', 'change_user', 'add_video', 'change_video', 'delete_video', 'add_documents', 'change_documents', 'delete_documents', 'add_news', 'change_news', 'delete_news', 'add_basics', 'change_basics', 'add_board', 'change_board', 'delete_board', 'add_history', 'change_history', 'delete_history', 'add_shortcuts', 'change_shortcuts', 'delete_shortcuts',])
permissions_qs.count()
>>> 24

And I saved the file 0001.initial.py and run python manage.py migrate but nothing did.

How I forced run again?

[UPDATE 3]

The complete code, and works very well.

# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-10-16 13:48
from __future__ import unicode_literals

from django.db import migrations, models
from django.contrib.auth.models import Group, Permission
from django.contrib.auth.management import create_permissions

def add_group_permissions(apps, schema_editor):
    for app_config in apps.get_app_configs():
        create_permissions(app_config, apps=apps, verbosity=0)

    # Criando Administrador
    group, created = Group.objects.get_or_create(name='Administrador')
    if created:
        permissions_qs = Permission.objects.filter(
                         codename__in=['add_permission',
                                                    'change_permission',
                                                     'add_user',
                                                     'change_user',
                                                     'add_video',
                                                     'change_video',
                                                     'delete_video',
                                                     'add_documents',
                                                     'change_documents',
                                                     'delete_documents',
                                                     'add_news',
                                                     'change_news',
                                                     'delete_news',
                                                     'add_basics',
                                                     'change_basics',
                                                     'add_board',
                                                     'change_board',
                                                     'delete_board',
                                                     'add_history',
                                                     'change_history',
                                                     'delete_history',
                                                     'add_shortcuts',
                                                     'change_shortcuts',
                                                     'delete_shortcuts',]
        )
        group.permissions = permissions_qs
        group.save()

class Migration(migrations.Migration):

    dependencies = [
        ('cursos', '0001_initial')
    ]

    operations = [
        migrations.RunPython(add_group_permissions),
    ]

After update my code I ran, like a tip @Trilliput:

  • manage.py migrate login zero --fake
  • remove the group
  • manage.py migrate login
Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
Juliano Araújo
  • 3,548
  • 1
  • 11
  • 16

1 Answers1

2

I assume, that your new Group must have all the permissions. Your query is wronge:

    add_thing = Permission.objects.get(
                     codename=['can_add_permision',
                               ...
                               'can_delete_shortcuts',]
    )

With objects.get you're requesting one Permission with a codename equals the given list, which is impossible, because codename is a string. You need IN request Permission.objects.filter(codename__in=['can_add_permision', ...]). And instead of group.permissions.add(add_thing) you may want to use group.permissions = qs. But pay attention, that permissions will overwritten. Here is a full code:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-10-16 13:48
from __future__ import unicode_literals

from django.db import migrations, models
from django.contrib.auth.models import Group, Permission
from django.contrib.auth.management import create_permissions


def add_group_permissions(apps, schema_editor):
    for app_config in apps.get_app_configs():
        create_permissions(app_config, apps=apps, verbosity=0)

    # Criando Administrador
    group, created = Group.objects.get_or_create(name='Administrador')
    if created:
        permissions_qs = Permission.objects.filter(
            codename__in=['can_add_permision',
                          'can_change_permission',
                          'can_add_user',
                          'can_change_user',
                          'can_add_video',
                          'can_change_video',
                          'can_delete_video',
                          'can_add_documents',
                          'can_change_documents',
                          'can_delete_documents',
                          'can_add_news',
                          'can_change_news',
                          'can_delete_news',
                          'can_add_basics',
                          'can_change_basics',
                          'can_add_board',
                          'can_change_board',
                          'can_delete_board',
                          'can_add_history',
                          'can_change_history',
                          'can_delete_history',
                          'can_add_shortcuts',
                          'can_change_shortcuts',
                          'can_delete_shortcuts',]
        )
        group.permissions = permissions_qs
        group.save()
        logger.info('Grupo Administrador Criado')

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.RunPython(add_group_permissions),
    ]
Shmygol
  • 913
  • 7
  • 16
  • Works partially 'cuz the group was create empty. Thx! – Juliano Araújo Oct 16 '17 at 18:42
  • @JulianoAraújo Make sure, that you do really have all the permissions. Try to call in the the Django console `Permissions.objects.filter(codename__in=['can_add_permision', ... , 'can_delete_shortcuts',]).count()` – Shmygol Oct 16 '17 at 18:54
  • @JulianoAraújo And by the way if you want all the permissions you could use just `group.permissions = Permissions.objects.all()` – Shmygol Oct 16 '17 at 19:01
  • It isn't all permission, are some. For example this group can't `can_delete_permission`. I'll try see in console. – Juliano Araújo Oct 16 '17 at 19:07
  • I used your tip about try in console... and I have the results of Update 2. – Juliano Araújo Oct 16 '17 at 19:48
  • Did you try only two of codes? When I wrote '...' I ment all the codenames you have in the migration, sorry. – Shmygol Oct 16 '17 at 19:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156859/discussion-between-juliano-araujo-and-trilliput). – Juliano Araújo Oct 16 '17 at 19:57