1
我有一個模型的DateField工作得很好。我想將它從DateField更改爲CharField。更改Django模型屬性,現在得到它的錯誤
前:
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) [email protected]:~/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'
我經歷了,並刪除了我的所有實例從我的所有車型,只是櫃面剩餘「NWEAScore」實例與test_date被扔的錯誤。
我在整個項目中搜索了術語「test_date」,試圖刪除每個實例,而剩下的只有在舊遷移中的實例。我應該回去刪除那些嗎?那可以拋棄嗎?如果是這樣,我不清楚如何擦除遷移並創建新遷移。
我給了它一個鏡頭幾天,如果有人有提前感謝的建議!
要爭取 - 即使是0001_initial.py,對不對? –
@AlexTrost是的,那也是 –
刪除它並運行遷移。它創造了一個新的0001_initial.py,給了我這樣的: '(牛頓)TrostMacbook:牛頓alexandertrost $蟒蛇manage.py makemigrations 遷移爲 'nwea': nwea /遷移/ 0001_initial.py: - 創建模型NWEAScore - 創建模型nWEASkill - 創建模型RITBand - 添加字段rit_band到nweaskill - 改變unique_together爲nweascore(1個約束(S))' –