[英]Django manage.py migrate ValidationError
While running django when I use python manage.py migrate
I am encountering the following error after running python manage.py makemigrations
当我使用
python manage.py migrate
运行 django 时,我在运行python manage.py makemigrations
后遇到以下错误
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/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 "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/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 "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
field,
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 433, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 161, in column_sql
default_value = self.effective_default(field)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 789, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1429, in get_db_prep_value
value = self.get_prep_value(value)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1408, in get_prep_value
value = super().get_prep_value(value)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1268, in get_prep_value
return self.to_python(value)
File "/Users/rasikraj/Desktop/eb-virt/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1393, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
What could be the cause of this error and How can I ressolve this issue.此错误的原因可能是什么以及如何解决此问题。 I'm using python 3.7 and Django 2.2
我正在使用 python 3.7 和 Django 2.2
Based on the error, there is a DateTimeField
[Django-doc] with as default=''
[Django-doc] .根据错误,有一个
DateTimeField
[Django-doc] with as default=''
[Django-doc] 。 But that is not a valid default: a DateTimeField
should have store a datetime in the database (or None
in case the field is NULL
-able).但这不是一个有效的默认值:
DateTimeField
应该在数据库中存储一个日期时间(或者None
以防该字段为NULL
)。
You thus should provide a valid default for that DateTimeField
, for example:因此,您应该为该
DateTimeField
提供一个有效的默认值,例如:
from django.utils import timezone
class SomeModel(models.Model):
my_field = DateTimeField(
default=timezone.now)
or you can omit the default value, and run makemigrations
.或者您可以省略默认值,并运行
makemigrations
。 In that case Django will ask if you want to provide a one-off default that will be applied to the records already in the database.在这种情况下,Django 会询问您是否要提供一次性默认值,该默认值将应用于数据库中已有的记录。
When you have changed the field you will need to remove the (invalid) migration file that was constructed where adding (or changing) that field took place, and make new migrations with manage.py makemigrations
.更改字段后,您需要删除在添加(或更改)该字段时构建的(无效)迁移文件,并使用
manage.py makemigrations
进行新迁移。 After all, this validation does not occur when constructing the migration files, and now that you migrate it will indeed error.毕竟,在构建迁移文件时不会发生这种验证,现在您迁移它确实会出错。 If you later change your model, the invalid migration fill is still not performed, hence it will raise the same error again.
如果稍后更改模型,仍然不会执行无效的迁移填充,因此它会再次引发相同的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.