Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving ValidationError: [u"'' value has an invalid date format. It must be in YYYY-MM-DD format."] in Django 1.9.2?

Earlier I created two fields & migrated everything. after that I tried to add three fields title,about,birthdate into the model. I created a model like this :

from __future__ import unicode_literals
from django.utils import timezone
from django.db import models

# Create your models here.

class APP1Model(models.Model):
    name = models.CharField(max_length=120)
    percentage = models.CharField(max_length=120)
    title = models.CharField(max_length=100,default='Title')
    birth_date = models.DateTimeField(blank=True, null=True)
    about = models.TextField(max_length=100,null=True,default='About Yourself')

    def __unicode__(self):
        return self.name

But when I try to migrate in python shell, it is showing a validation error like this:

Operations to perform:
  Apply all migrations: admin, contenttypes, auth, app1, sessions
Running migrations:
  Applying app1.0005_auto_20160217_1346...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 221, in add_field
    self._remake_table(model, create_fields=[field])
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 103, in _remake_table
    self.effective_default(field)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 210, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
    prepared=False)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1301, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1296, in get_prep_value
    return self.to_python(value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1273, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: [u"'' value has an invalid date format. It must be in YYYY-MM-DD format."]

How to rectify this? I tried all solution i read here but it doesn't work?

I am using Django: 1.9.2

My migration File

from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

dependencies = [
    ('app1', '0004_auto_20160217_0427'),
]

operations = [
    migrations.AddField(
        model_name='app1model',
        name='about',
        field=models.TextField(default='About Yourself', max_length=100, null=True),
    ),
    migrations.AddField(
        model_name='app1model',
        name='birth_date',
        field=models.DateField(blank=True, default='', null=True),
    ),
    migrations.AddField(
        model_name='app1model',
        name='title',
        field=models.CharField(default='', max_length=100),
    ),
]
like image 591
Peniel Avatar asked Feb 18 '16 05:02

Peniel


2 Answers

I went through the same problem some months back.I Just deleted the birthdate field changes in all the migration Files inside migration folder. Then I replaced the birthdate with this code:-

birthdate = models.DateTimeField(blank=True, null=True)                            

Then after applying migration ,it works fine...

like image 141
IamLitto Avatar answered Nov 15 '22 10:11

IamLitto


It seems you have passed DateTimeField

birth_date = models.DateTimeField(blank=True, null=True)

in your APP1Model(model) and migration shows DateFields

models.DateField(blank=True, default='', null=True)

first correct your Model and don't pass default='' in DateField,

Use DateField instead of DatiTimeField for birthdate

then remove your migration file app1.0005_auto_20160217_1346

and run makemigrations and migrate your app it'll work fine.

like image 3
Kamlesh Avatar answered Nov 15 '22 11:11

Kamlesh