如何在Django 1.11.2中自动创建组

时间:2017-10-16 17:50:24

标签: python django

我尝试按照本教程“Programmatically creating a group : Can't access permissions from migration”进行操作,我的代码是:

# -*- 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),
    ]

我的错误是:

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

问题可能与dependencies有关吗?或者我怎么做?

[更新1]

在@Trilliput的提示之后,该组被创建为空。

enter image description here

[更新2]

我在使用@Trilliput的提示后,使用shell can_

删除了前缀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

我保存了文件0001.initial.py并运行python manage.py migrate,但没有做任何事。

我怎么强行再跑?

[更新3]

完整的代码,效果很好。

# -*- 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),
    ]

在更新我的代码后,我跑了,就像提示@Trilliput:

  • manage.py migrate login zero --fake
  • 删除论坛
  • manage.py migrate login

1 个答案:

答案 0 :(得分:2)

我认为您的新组必须拥有所有权限。您的查询是wronge:

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

使用objects.get,您要求一个权限等同于给定列表的权限,这是不可能的,因为代号是一个字符串。您需要IN请求Permission.objects.filter(codename__in=['can_add_permision', ...])。而不是group.permissions.add(add_thing)您可能想要使用group.permissions = qs。但请注意,权限将被覆盖。这是完整的代码:

# -*- 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),
    ]