Django Cant访问自定义数据迁移中的auth User,Group对象

时间:2015-06-23 10:51:03

标签: django django-migrations

我的迁移文件如下所示:

from __future__ import unicode_literals

from django.db import migrations
from project.tools import do_nothing

def create_can_receive_group(apps, schema_editor):
    Group = apps.get_model("django.contrib.auth", 'Group')
    # Group operation added here

class Migration(migrations.Migration):

    dependencies = [
        ('poreceiving', '0004_auto_20150616_0846'),
        ('django.contrib.auth', '0006_require_contenttypes_0002')
    ]

    operations = [
        migrations.RunPython(create_can_receive_group, do_nothing),
    ]

在这里,我想访问 django.contrib.auth 的Group对象。 我得到以下异常。

*** LookupError: No installed app with label 'django.contrib.auth'.

我发现某个地方,如果我们想要使用其他不存在迁移的app中的对象,那么我们应该添加其他app的最新迁移。

当我将 django.contrib.auth 最新迁移添加到我得到的依赖项时:

django.db.migrations.graph.NodeNotFoundError: Migration poreceiving.0005_create_can_receive_group dependencies reference nonexistent parent node (u'django.contrib.auth', u'0006_require_contenttypes_0002')

1 个答案:

答案 0 :(得分:0)

尝试这样的事情(查看依赖项中的migrations.swappable_dependency部分):

from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations
from project.tools import do_nothing


def create_can_receive_group(apps, schema_editor):
    Group = apps.get_model("auth", "Group")
    # Group operation added here


class Migration(migrations.Migration):

    dependencies = [
        ('poreceiving', '0004_auto_20150616_0846'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.RunPython(create_can_receive_group, do_nothing),
    ]