Alex Trost Alex Trost - 1 year ago 82
Python Question

Changed Django model attribute and now getting error for it

I had a model with a DateField that worked just fine. I wanted to change it from a DateField to a CharField.


class NWEAScore(models.Model):

test_date = models.DateField(, 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)

Not sure what went wrong but now I'm getting an error.

Making migrations is no problem. I make them and then upload them to my server, then when I migrate, I get an error.
The Error I get when I try to apply my migrations:

(venv) alex@newton:~/newton$ python 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/", 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 "", line 10, in <module>
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/", line 367, in execute_from_command_line
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/", line 359, in execute
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/", line 356, in execute
output = self.handle(*args, **options)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/core/management/commands/", 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/", 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/", 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/", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/alex/newton/venv/lib/python3.4/site-packages/django/db/migrations/", 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/", 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/", 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/", 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/", 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/", 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'

I went through and deleted all my instances from all my models, just incase a remaining "NWEAScore" Instance with a test_date was throwing the error.

I searched through my entire project for the term "test_date", trying to delete every instance, and the only remaining instances are in old migrations. Should I go back and delete those? Could that be throwing that off? I'm not too clear on how to wipe migrations and make new ones if that's it.

I gave it a shot for a few days, if anyone has suggestions thanks in advance!

Answer Source

Go to the migrations folder of your app and delete all files, except Then run the command:

python makemigrations

to make new migrations with the updated fields

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download