早些时候我创建了两个字段&迁移了一切。之后,我尝试将三个字段title
,about
,birthdate
添加到模型中。
我创建了一个这样的模型:
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中迁移时,它会显示如下的验证错误:
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."]
如何纠正这个?我尝试了我在这里阅读的所有解决方案,但它不起作用?
我正在使用Django:1.9.2
我的迁移文件
from __future__ import unicode_literals
from django.db import migrations, models
类迁移(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),
),
]
答案 0 :(得分:6)
几个月前我遇到了同样的问题。我刚刚删除了迁移文件夹中所有迁移文件中的birthdate字段更改。然后我用这段代码替换了生日: -
birthdate = models.DateTimeField(blank=True, null=True)
然后在应用迁移后,它工作正常......
答案 1 :(得分:1)
您似乎已通过DateTimeField
birth_date = models.DateTimeField(blank=True, null=True)
您的APP1Model(模型)中的和迁移显示DateFields
models.DateField(blank=True, default='', null=True)
首先更正您的模型并且不要通过default='' in DateField,
使用DateField
代替DatiTimeField
生日
然后删除您的迁移文件app1.0005_auto_20160217_1346
并运行 makemigrations 并迁移您的应用程序,它将正常运行。
答案 2 :(得分:0)
将DateField
更改为DateTimeField
并不是一个坏主意。虽然有DateField
和DateTimeField
,但由于某些原因这些不同。
当DateField
中的models.py
以错误的格式应用默认值时会发生此错误。
根据您的终端输出,迁移文件0005_auto_20160217_1346.py
中会出现错误。您必须在&#39; app1 / 0005_auto_20160217_1346.py`中找到该迁移文件,对其进行编辑并查找:
...
migrations.AddField(
model_name='app1model',
name='birth_date',
field=models.DateField(blank=True, default='<jamming-date>', null=True),
),
...
现在,您可以看到DateField
default
属性包含<jamming-date>
格式为%d-%m-%Y
的{{1}}。 Django需要%Y-%m-%d
格式。
要解决此问题,删除 default
属性,保存文件,再次迁移。
答案 3 :(得分:0)
只需在迁移文件中删除default =''并运行migration即可。
之前:
migrations.AddField(
model_name='formd',
name='cash_payment_date',
field=models.DateField(blank=True, default='', help_text='Date of Cash Payment', null=True),
),
之后:
migrations.AddField(
model_name='formd',
name='cash_payment_date',
field=models.DateField(blank=True, help_text='Date of Cash Payment', null=True),
),
已删除的默认=''
Ran移民
答案 4 :(得分:0)
尝试将DateTime的默认属性从空白“(在我的初始迁移文件中设置)”更改为django.utils.timezone.now(在以后的迁移中)后,我遇到了同样的错误。您需要返回并编辑0001_Initial.py文件,并将默认值更改为现在想要的任何值。似乎在应用迁移时,Django正在参考该初始文件进行验证并抛出错误,这可能会产生误导,我花了几个小时试图弄清原因……希望我可以节省一些时间:)< / p>