本文介绍了如何在django迁移期间向用户/组添加权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想执行以下迁移:
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.contrib.auth.models import Permission from django.db import migrations from django.conf import settings from django.contrib.auth.models import Group, User def add_api_group(apps, schema_editor): Group.objects.create(name=settings.API_USER_GROUP) # get_or_create returns a tuple, not a Group group = Group.objects.get(name=settings.API_USER_GROUP) permissions = Permission.objects.filter(codename__in = [ 'add_topic', ]) group.permissions.add(*permissions) def add_api_user(apps, schema_editor): user = User.objects.create_user(username=settings.API_USER, password=settings.API_USER_PASSWORD) group = Group.objects.get(name=settings.API_USER_GROUP) user.groups.add(group) class Migration(migrations.Migration): dependencies = [ ('nd_content', '0001_initial'), ] operations = [ migrations.RunPython(add_api_group), migrations.RunPython(add_api_user) ]在迁移的最后一行,我发出了一个错误以停止执行并查看数据库状态.问题在于,表auth_permission仍然没有其他模块的模型的权限,尽管该其他模块已注册为该迁移的依赖项.
At the last line of the migration, I issued an error to stop execution and look at the database state. The problem is the table auth_permission still has not the permissions of a model of another module, although this other module is registered as a dependecy of this migration.
我可以确认似乎仅在执行所有迁移之后才添加缺少的权限.
I can confirm missing permissions seem to be added only after all migrations have been executed.
推荐答案AttributeError: 'StateApps' object has no attribute 'label' in Django 1.10
有一个解决方案:
for app_config in apps.get_app_configs(): app_config.models_module = True create_permissions(app_config, verbosity=0) app_config.models_module = None更多推荐
如何在django迁移期间向用户/组添加权限?
发布评论