如何在django迁移期间向用户/组添加权限?

编程入门 行业动态 更新时间:2024-10-25 19:31:08
本文介绍了如何在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迁移期间向用户/组添加权限?

本文发布于:2023-10-30 04:00:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:权限   用户   如何在   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!