Search code examples
djangopython-3.xdjango-modelsdjango-templatesdatefield

django.core.exceptions.ValidationError: ["'' value has an invalid date format. It must be in YYYY-MM-DD format."]


I using Django version 2.0.7 and I tried to make a Date Module Field Until I faced this error :

Date = models.DateField(blank=True, default='', null=True, help_text='today date.')

I used this code before and it worked but now after python manage.py makemigrations that I use python manage.py migrate I faced to this error. full logs:

Operations to perform:
  Apply all migrations: Blog, Portfolio, admin, auth, contenttypes, sessions
Running migrations:
  Applying Blog.0002_auto_20180728_0218...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\core\management\__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\core\management\base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\migrations\migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\migrations\operations\fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\backends\base\schema.py", line 525, in alter_field
    old_db_params, new_db_params, strict)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\backends\postgresql\schema.py", line 122, in _alter_field
    new_db_params, strict,
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\backends\base\schema.py", line 629, in _alter_field
    old_default = self.effective_default(old_field)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\backends\base\schema.py", line 240, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\models\fields\__init__.py", line 767, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\models\fields\__init__.py", line 1276, in get_db_prep_value
    value = self.get_prep_value(value)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\models\fields\__init__.py", line 1271, in get_prep_value
    return self.to_python(value)
  File "D:\Learn\Projects\Django\myvenv\lib\site-packages\django\db\models\fields\__init__.py", line 1246, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: ["'' value has an invalid date format. It must be in YYYY-MM-DD format."]

I even try to remove all migrations and db and make new migrationsbut it doesn't work too. this link doesnt work for me too : Resolving ValidationError: [u“'' value has an invalid date format. It must be in YYYY-MM-DD format.”] in Django 1.9.2?

In Addision, I'm sorry for writing mistakes in my question.


Solution

  • The default can not be the empty string, since that is not a Date. for example use date(1111, 11, 11) with from datetime import date in modals page then if you will be get same error , you have to delete all migrations and erase your database data too and do migrations again.