本文介绍了解决ValidationError:[u''值的日期格式无效.它必须为YYYY-MM-DD格式.&“]在Django 1.9.2中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前,我创建了两个字段&迁移了一切.之后,我尝试将三个字段 title about birthdate 添加到模型中.我创建了一个这样的模型:

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

但是当我尝试在python shell中迁移时,它显示了这样的验证错误:

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?

我正在使用Django:1.9.2

I am using Django: 1.9.2

我的迁移文件

from __future__ import unicode_literals

from django.db import migrations, models

类迁移(migrations.Migration):

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),
    ),
]

推荐答案

几个月前,我遇到了同样的问题.我只是删除了迁移文件夹中所有迁移文件中的发生日期字段更改.然后我用以下代码替换了生日:-

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...

这篇关于解决ValidationError:[u&amp;#39;&amp;#39;值的日期格式无效.它必须为YYYY-MM-DD格式.&amp;“]在Django 1.9.2中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 05:05