Reputation: 733
I need to populate auth_group_permission table in a migration. I've created a migration with this code:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-12-05 10:07
from __future__ import unicode_literals
from django.db import migrations
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)
#Workers
group, created = Group.objects.get_or_create(name='Workers')
can_add_worker_task = Permission.objects.get(codename='add_worker_task')
group.permissions.add(can_add_worker_task)
group.save()
class Migration(migrations.Migration):
dependencies = [
('crowdsourcing', '0005_add_view_permission'),
('auth', '0001_initial'),
]
operations = [
migrations.RunPython(add_group_permissions),
]
But when I execute "python manage.py migrate" commando I received this error: "django.contrib.auth.models.DoesNotExist: Permission matching query does not exist". I think the problem is that the "auth_permission" table is still empty. Can i solve?
Upvotes: 0
Views: 1239
Reputation: 733
I've solve with this code:
def add_group_permissions(apps, schema_editor):
for app_config in apps.get_app_configs():
app_config.models_module = True
create_permissions(app_config, apps=apps, verbosity=0)
app_config.models_module = None
Upvotes: 2