我有一个这样的模型文件:

from django.db import models

class Gin(models.Model):
    objectId = models.CharField(max_length=100, unique=True)
    alcohol = models.FloatField(null=True, blank=True, default=None)
    name = models.CharField(max_length=100)
    picture = models.CharField(max_length=200)

class GinLocal(models.Model):
    objectId = models.CharField(max_length=100)
    origin = models.TextField()
    serve = models.TextField()
    aroma = models.TextField()


运行以下终端命令后,将添加第二个类GinLocal:

python manage.py makemigrations ginsdb
python manage.py migrate ginsdb


第一次运行良好,第二次添加第二个模型时,我总是在migration命令上收到此错误:

(myvenv) ➜  gins4udjango python manage.py migrate ginsdb
Operations to perform:
  Apply all migrations: ginsdb
Running migrations:
  Rendering model states... DONE
  Applying ginsdb.0002_ginlocal...Traceback (most recent call last):
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/options.py", line 580, in get_field
    return self.fields_map[field_name]
KeyError: <django.db.models.fields.CharField>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 235, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 134, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 967, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 958, in db_type
    rel_field = self.target_field
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 861, in target_field
    return self.foreign_related_fields[0]
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 594, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 581, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 574, in resolve_related_fields
    else self.remote_field.model._meta.get_field(to_field_name))
  File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/options.py", line 582, in get_field
    raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Gin has no field named <django.db.models.fields.CharField>
(myvenv) ➜  gins4udjango


有人有主意吗?我没看到问题。

编辑:

Latest migration file

# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2016-04-23 10:45
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('ginsdb', '0007_delete_ginlocal'),
    ]

    operations = [
        migrations.CreateModel(
            name='GinLocal',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('objectId', models.CharField(max_length=100)),
                ('origin', models.TextField()),
                ('serve', models.TextField()),
                ('aroma', models.TextField()),
            ],
        ),
    ]

最佳答案

如错误日志中所述,问题出在:位于ginsdb.0002_ginlocal...中的../Path_To_Your_App/ginsdb/migrations

您有2个选择:

1-如果数据库为空,而您没有输入任何内容,则可以简单地创建一个新数据库,在settings.py中更改数据库,在../Path_To_Your_App/ginsdb/migrations中清除所有迁移,然后尝试以下操作:

>>>python manage.py makemigrations
>>>python manage.py migrate ginsdb


2-如果您不想更改数据库,则应查找错误原因,因为开始时应将所有文件发布到../Path_To_Your_App/ginsdb/migrations/
并说出设计模型的步骤。

在某些情况下,只需删除../Path_To_Your_App/ginsdb/migrations/ginsdb.0002_ginlocal...

关于python - Django模型无法迁移,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36809775/

10-10 10:47