我有一堆新的权限,我需要迁移.我尝试通过数据迁移来做,但抱怨ContentType not being available.
快速研究我发现ContentType在应用所有迁移后填充表.
我甚至尝试使用update_all_contenttypes()从中from django.contrib.contenttypes.management import update_all_contenttypes
导致迁移加载与夹具不一致的数据.
在Django中迁移权限数据的最佳方法是什么?
view_amodel我正在尝试向我的模型添加权限。我决定在迁移后添加权限。所以我采取了以下方法。
在 an_app / init.py
from an_app.apps import MyAppConfig
default_app_config = MyAppConfig
Run Code Online (Sandbox Code Playgroud)
在 an_app/apps.py
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def add_view_permissions(sender, **kwargs):
"""
This syncdb hooks takes care of adding a view permission too all our
content types.
"""
# for each of our content types
for content_type in ContentType.objects.all():
# build our permission slug
codename = "view_%s" % content_type.model
# if it doesn't exist..
if not Permission.objects.filter(content_type=content_type, …Run Code Online (Sandbox Code Playgroud)