Django 在外键中使用用户迁移模型失败

标签 django migration django-users

我正在尝试迁移我的 django 模型:

from django.contrib.auth.models import User


class Post(models.Model):
    headline = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200)
    body = models.TextField(blank=True, null=True)
    author = models.ForeignKey(User, null=True, blank=True)

我在创建模型后添加了作者字段。

这是 django 创建的迁移:

# encoding: utf8
from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [('articles', '0002_auto')]

    operations = [
        migrations.AddField(
            field = models.ForeignKey(to_field=u'id', to=u'auth.User', blank=True, null=True),
            name = 'author',
            model_name = 'post',
        ),
    ]

这是我尝试运行 ./manage.py migrate 时的回溯:

Operations to perform:
  Synchronize unmigrated apps: ckeditor, sessions, admin, messages, auth, staticfiles, contenttypes, django_extensions
  Apply all migrations: articles
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Installed 0 object(s) from 0 fixture(s)
Running migrations:
  Applying articles.0002_post_author...Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/core/management/__init__.py", line 397, in execute_from_command_line
    utility.execute()
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/core/management/__init__.py", line 390, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/core/management/base.py", line 289, in execute
    output = self.handle(*args, **options)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/core/management/commands/migrate.py", line 116, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/migrations/executor.py", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/migrations/executor.py", line 73, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/migrations/migration.py", line 80, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/migrations/operations/fields.py", line 22, in database_forwards
    schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/backends/schema.py", line 349, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/backends/schema.py", line 105, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/models/fields/related.py", line 1285, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/models/fields/related.py", line 1276, in db_type
    rel_field = self.related_field
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/models/fields/related.py", line 1183, in related_field
    return self.foreign_related_fields[0]
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/models/fields/related.py", line 971, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/models/fields/related.py", line 958, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/home/USER/.virtualenvs/PROJECT/src/django-trunk/django/db/models/fields/related.py", line 943, in resolve_related_fields
    raise ValueError('Related model %r cannot been resolved' % self.rel.to)
ValueError: Related model u'auth.User' cannot been resolved

有人知道我做错了什么吗?

最佳答案

在这种情况下什么对我有帮助:

  1. 删除除 __init__.py(/%prjname%/migrations 文件夹)之外的所有迁移文件
  2. python manage.py makemigrations
  3. python manage.py 迁移

不确定确切原因,但我尝试使用由我的代码合作伙伴生成的文件,但没有成功。

关于Django 在外键中使用用户迁移模型失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18601494/

相关文章:

python - 如何使用 django 表单编辑模型数据

Django 使用 CheckboxSelectMultiple 小部件设置 <ul> 类而不是 &lt;input&gt;

ruby-on-rails - Rails Migrations 可以用来转换数据吗?

scala - 玩迁移替代方案

python - Django 跳过特定的迁移(没有要应用的迁移)

django - 从 Django Auth 中的 UserChangeForm 中排除用户名或密码

python - Django - GenericForeignKey 的动态表单

django - 如何在 Django 中提供用户上传的文件?

python - Django:自动创建用户配置文件时,它的设置是什么?

python - Django 自定义用户模型和用户管理器