django:数据迁移权限 [英] django: data migrate permissions

查看:151
本文介绍了django:数据迁移权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆我需要迁移的新权限。我尝试通过数据迁移进行,但抱怨 ContentType不可用



快速研究我发现

我甚至尝试使用 update_all_contenttypes( )从django.contrib.contenttypes.management导入update_all_contenttypes
导致迁移加载与固定装置不一致的数据。



在Django中迁移权限数据的最佳方式是什么?

解决方案

p>有两种解决方法:



1)丑陋的方式:



运行 manage.py migration auth 在您想要的迁移之前



2)推荐方法:

  from django.contrib.auth.management import create_permissions 

def add_permissions(apps,schema_editor):
apps.models_module = True

create_permissions(apps,verbosity = 0)
apps.models_module =无

#其余的代码在这里....


I have a bunch of new permissions which I need to migrate. I tried doing it through data migration but complains about ContentType not being available.

Doing quick research I found out that ContentType table is populated after all the migrations applied.

I even tried using update_all_contenttypes() from from django.contrib.contenttypes.management import update_all_contenttypes which causes migration to load data which is not consistent to the fixture.

What is the best way to migrate permission data in Django?

解决方案

There are 2 ways to solve this:

1) The ugly way:

Run manage.py migrate auth before your wanted migration

2) Recommended way:

from django.contrib.auth.management import create_permissions

def add_permissions(apps, schema_editor):
    apps.models_module = True

    create_permissions(apps, verbosity=0)
    apps.models_module = None

    # rest of code here....

这篇关于django:数据迁移权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆