django:数据迁移权限

2023-11-21

我有很多需要迁移的新权限。我尝试通过数据迁移来做到这一点,但抱怨ContentType not being available.

快速研究后我发现ContentType应用所有迁移后将填充表。

我什至尝试使用update_all_contenttypes() from from django.contrib.contenttypes.management import update_all_contenttypes 这会导致迁移加载与夹具不一致的数据。

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


有两种方法可以解决这个问题:

1)丑陋的方式:

Run manage.py migrate 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 = None

    # rest of code here....
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

django:数据迁移权限 的相关文章

随机推荐