对于CustomKey,Django迁移失败到自定义用户模型

时间:2014-10-23 11:57:25

标签: django django-1.7 django-migrations

当我为具有自定义用户模型的ForeignKey的新模型运行迁移时,出现错误:

ValueError: Lookup failed for model referenced by field test.TestModel.user: account.EmailUser

它在Django 1.7中。

我的自定义用户为apps.models.EmailUser

以下是相关代码:

# apps.test/models.py
from django.db import models
from django.conf import settings


class TestModel(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

# settings.py

AUTH_USER_MODEL = 'account.EmailUser'
INSTALLED_APPS = (
    ...
    'apps.account',
    'apps.test',
)

最后,这是迁移文件:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='TestModel',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
    ]

完整错误追溯

Running migrations:
  Applying test.0001_initial...Traceback (most recent call last):
  File "./manage.py", line 4, in <module>
    manage_environment()
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/bobsleigh/runner/__init__.py", line 9, in manage_environment
    execute_from_command_line(sys.argv)
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 91, in apply_migration
    if self.detect_soft_applied(migration):
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 135, in detect_soft_applied
    apps = project_state.render()
  File "/home/david/.virtualenvs/lingoing/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 89, in render
    model=lookup_model,
ValueError: Lookup failed for model referenced by field test.TestModel.user: account.EmailUser

额外信息

我注意到有一个测试,看它是否在django.db.migrations.state.render()中使用自定义用户模型,但是如果使用ignore_swappable = True调用render,它只会认真对待此测试不是我的情况。

任何想法?

0 个答案:

没有答案