更改了Django模型属性,现在为其获取错误

时间:2016-09-27 21:07:52

标签: python django django-models

我有一个带有DateField的模型,效果很好。我想将它从DateField更改为CharField。

之前:

class NWEAScore(models.Model):

    test_date = models.DateField(default=date.today, verbose_name='Test Date')

后:

class NWEAScore(models.Model):

    year = models.CharField(max_length=50, choices=YEAR_CHOICES, default=SIXTEEN)
    season = models.CharField(max_length=50, choices=SESSION_CHOICES, default=FALL)

不确定出了什么问题,但现在我收到了错误。

进行迁移是没有问题的。我制作它们然后将它们上传到我的服务器,然后当我迁移时,我收到一个错误。 我尝试应用迁移时遇到的错误:

(venv) alex@newton:~/newton$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, amc, auth, brain, contenttypes, ixl, nwea, sessions
Running migrations:
Rendering model states... DONE
Applying brain.0021_auto_20160927_0038... OK
Applying nwea.0011_auto_20160927_0038...Traceback (most recent call last):
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/models/options.py", line 612, in get_field
return self.fields_map[field_name]
KeyError: 'test_date'

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 "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 97, in migrate
state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 132, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/migrations/operations/models.py", line 525, in database_forwards
getattr(new_model._meta, self.option_name, set()),
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 329, in alter_unique_together
self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 352, in _delete_composed_index
columns = [model._meta.get_field(field).column for field in fields]
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 352, in <listcomp>
columns = [model._meta.get_field(field).column for field in fields]
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/models/options.py", line 614, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: NWEAScore has no field named 'test_date'

我经历了并删除了我所有模型中的所有实例,只是包含剩余的&#34; NWEAScore&#34;带有test_date的实例抛出了错误。

我在整个项目中搜索了术语&#34; test_date&#34;,尝试删除每个实例,剩下的唯一实例是旧迁移。我应该回去删除那些吗?可能会抛弃它吗?我不太清楚如何擦除迁移并制作新的迁移(如果有的话)。

如果有人事先提出建议,我试了几天!

1 个答案:

答案 0 :(得分:1)

转到应用的迁移文件夹并删除除this._sessionService = new TempService(this); 以外的所有文件。然后运行命令:

__init__.py

使用更新的字段进行新的迁移