在Django 1.8项目中,当有以下代码时,我的迁移工作正常:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
from django.conf import settings
def update_site_forward(apps, schema_editor):
"""Add group osmaxx."""
Group = apps.get_model("auth", "Group")
Group.objects.create(name=settings.OSMAXX_FRONTEND_USER_GROUP)
def update_site_backward(apps, schema_editor):
"""Revert add group osmaxx."""
Group = apps.get_model("auth", "Group")
Group.objects.get(name=settings.OSMAXX_FRONTEND_USER_GROUP).delete()
class Migration(migrations.Migration):
dependencies = [
('auth', '0001_initial'),
]
operations = [
migrations.RunPython(update_site_forward, update_site_backward),
]
Run Code Online (Sandbox Code Playgroud)
此组是在迁移中创建的,因为它应在Web应用程序的所有安装中可用.为了使它更有用,我还想给它一个默认权限,所以我改为update_site_forward:
def update_site_forward(apps, schema_editor):
"""Add group osmaxx."""
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
ContentType = apps.get_model("contenttypes", "ContentType")
ExtractionOrder …Run Code Online (Sandbox Code Playgroud) 我想在手动编写的迁移中添加一些组并为其分配权限,但如果我在干净的DB上运行它,则只有在运行所有迁移后才会创建权限.
我找到了这张票:https: //code.djangoproject.com/ticket/23422但我不能在那里发表评论(我可能会在表达对GeoDjango文档的不满之后被禁止),所以我将分享对解决方案的改进在下面.