Skip to main content
 首页 » 编程设计

python之更改了 Django 模型属性,现在收到错误

2024年10月17日33www_RR

我有一个带有 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' 

我检查并删除了我所有模型中的所有实例,以防万一带有 test_date 的剩余“NWEAScore”实例抛出错误。

我在整个项目中搜索了术语“test_date”,试图删除每个实例,而唯一剩下的实例在旧迁移中。我应该回去删除那些吗?那能把它扔掉吗?如果仅此而已,我不太清楚如何清除迁移并进行新迁移。

我尝试了几天,如果有人有建议,请提前致谢!

请您参考如下方法:

转到您应用的migrations 文件夹并删除所有文件,__init__.py 除外。然后运行命令:

python manage.py makemigrations 

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