Home > Software design >  TypeError: fromisoformat: argument must be str
TypeError: fromisoformat: argument must be str

Time:10-21

I have accidentally made '1' the default value for the models.DateField(). Now Django throws an error everytime I try to migrate, even if I delete the CharacterField / change the default value using (default=datetime.now()).

Is there a way to fix this?

Applying mainApp.0006_test_date...Traceback (most recent call last):
  File "/Users/di/Code/Schule/GymnasiumApp/manage.py", line 22, in <module>
    main()
  File "/Users/di/Code/Schule/GymnasiumApp/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 349, in handle
    post_migrate_state = executor.migrate(
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/migrations/migration.py", line 130, in apply
    operation.database_forwards(
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/migrations/operations/fields.py", line 108, in database_forwards
    schema_editor.add_field(
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/backends/sqlite3/schema.py", line 394, in add_field
    self._remake_table(model, create_field=field)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/backends/sqlite3/schema.py", line 237, in _remake_table
    self.effective_default(create_field),
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 423, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 925, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 1438, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 1433, in get_prep_value
    return self.to_python(value)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 1389, in to_python
    parsed = parse_date(value)
  File "/Users/di/Code/Schule/GymnasiumApp/venv/lib/python3.10/site-packages/django/utils/dateparse.py", line 74, in parse_date
    return datetime.date.fromisoformat(value)
TypeError: fromisoformat: argument must be str

CodePudding user response:

You can remove the 0006_*****.py migration in the dirs apps/app/migrations if it is the last one, and recreate the migration after change the default value in your field by using command makemigrations

If it is not the last one, you have to remove all next after 0006_***, change the default value of your DateField and recreate migrations by using command makemigrations. In this case, you probably need to destroy your db and recreate it for preventing other problems

CodePudding user response:

You have to change every '1' in your database to a correct value.

You can try something like UPDATE <table> SET <column>='2022-01-01' WHERE <column> LIKE '1';

You can open your database with the following command (on linux) :

sqlite3 db.sqlite3

UPDATE <table> SET <column>='2022-01-01' WHERE <column> LIKE '1';

It may also work with cmd :

sqlite3

.open db.sqlite3

UPDATE <table> SET <column>='2022-01-01' WHERE <column> LIKE '1';

  • Related