django.core.exceptions.ValidationError: ["'' value has an invalid date format. It must be in YYYY-MM-DD format."]
django.core.exceptions.ValidationError: ["'' value has an invalid date format. It must be in YYYY-MM-DD format."]
我使用 Django 版本 2.0.7 并尝试创建一个日期模块字段,直到遇到此错误:
Date = models.DateField(blank=True, default='', null=True, help_text='today date.')
我之前使用过此代码并且它有效但现在在 python manage.py makemigrations
我使用 python manage.py migrate
之后我遇到了这个错误。
完整日志:
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."]
我什至尝试删除所有迁移和数据库并进行新的迁移,但它也不起作用。
这个 link 对我也不起作用:
另外,很抱歉我的问题写错了
默认不能为空字符串,因为那不是日期。
例如,在模式页面中使用 date(1111, 11, 11)
和 from datetime import date
然后如果你会得到同样的错误,你必须删除所有迁移和擦除你的数据库数据并且做迁移又是。
import datetime
import pytz
from django.apps import AppConfig
from django.db.models.fields import DateTimeField
from django.utils.dateparse import parse_date
from django.core.exceptions import ValidationError
default_app_config = 'patch.PatchConfig'
def patch():
def get_db_prep_value(self, value, connection, prepared=False):
if isinstance(value, str):
if not value:
value = None
elif not parse_date(value):
try:
value = datetime.datetime.fromtimestamp(int(value)).replace(tzinfo=pytz.UTC)
except:
raise ValidationError(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
)
if not prepared:
value = self.get_prep_value(value)
return connection.ops.adapt_datetimefield_value(value)
DateTimeField.get_db_prep_value = get_db_prep_value
class PatchConfig(AppConfig):
name = 'patch'
def ready(self):
patch()
您可能需要这个补丁
我在更新 model.py
时遇到同样的格式错误
我给你看我的场景,也许它会有所帮助。所以我有一个现有的 class 并想添加这一行:
time = models.TimeField()
比我做的 makemigrations 现在重要的部分
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value
for this column)
2) Quit, and let me add a default in models.py
选择 1) 并键入 timezone.now 以设置默认值 --
迁移,你明白了
我使用 Django 版本 2.0.7 并尝试创建一个日期模块字段,直到遇到此错误:
Date = models.DateField(blank=True, default='', null=True, help_text='today date.')
我之前使用过此代码并且它有效但现在在 python manage.py makemigrations
我使用 python manage.py migrate
之后我遇到了这个错误。
完整日志:
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."]
我什至尝试删除所有迁移和数据库并进行新的迁移,但它也不起作用。
这个 link 对我也不起作用:
另外,很抱歉我的问题写错了
默认不能为空字符串,因为那不是日期。
例如,在模式页面中使用 date(1111, 11, 11)
和 from datetime import date
然后如果你会得到同样的错误,你必须删除所有迁移和擦除你的数据库数据并且做迁移又是。
import datetime
import pytz
from django.apps import AppConfig
from django.db.models.fields import DateTimeField
from django.utils.dateparse import parse_date
from django.core.exceptions import ValidationError
default_app_config = 'patch.PatchConfig'
def patch():
def get_db_prep_value(self, value, connection, prepared=False):
if isinstance(value, str):
if not value:
value = None
elif not parse_date(value):
try:
value = datetime.datetime.fromtimestamp(int(value)).replace(tzinfo=pytz.UTC)
except:
raise ValidationError(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
)
if not prepared:
value = self.get_prep_value(value)
return connection.ops.adapt_datetimefield_value(value)
DateTimeField.get_db_prep_value = get_db_prep_value
class PatchConfig(AppConfig):
name = 'patch'
def ready(self):
patch()
您可能需要这个补丁
我在更新 model.py
时遇到同样的格式错误我给你看我的场景,也许它会有所帮助。所以我有一个现有的 class 并想添加这一行:
time = models.TimeField()
比我做的 makemigrations 现在重要的部分
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value
for this column)
2) Quit, and let me add a default in models.py
选择 1) 并键入 timezone.now 以设置默认值 -- 迁移,你明白了