gpt4 book ai didi

python - Django 日期时间声明错误

转载 作者:行者123 更新时间:2023-12-01 02:17:31 24 4
gpt4 key购买 nike

我在 models.py 中添加了以下代码:

class Conference(models.Model):
conf_name = models.CharField(max_length=250)
location = models.CharField(max_length=500)
note = models.TextField(max_length=500)
note2 = models.TextField(max_length=500)
date_from = models.DateField()
date_to = models.DateField()
time_from = models.TimeField(default='HH:MM:ss')
time_to = models.TimeField(default='HH:MM:ss')

之后,我运行迁移命令并得到以下回溯:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'appadmin'
HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode
?: (urls.W005) URL namespace 'dev_app' isn't unique. You may not be able to reverse all URLs in this namespace
Operations to perform:
Apply all migrations: admin, auth, contenttypes, dev_app, sessions
Running migrations:
Applying dev_app.0016_auto_20180112_0630...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/lib/python2.7/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 86, in database_forwards
field,
File "/usr/lib/python2.7/site-packages/django/db/backends/mysql/schema.py", line 48, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File "/usr/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 414, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/usr/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 154, in column_sql
default_value = self.effective_default(field)
File "/usr/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 228, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save
prepared=False)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2277, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2272, in get_prep_value
return self.to_python(value)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2259, in to_python
params={'value': value},
django.core.exceptions.ValidationError: [u"'null' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."]

即使我恢复了 models.py 中的代码,回溯仍然存在。

我需要帮助理解回溯。

在应用模型更改之前,代码可以正常运行。模型迁移正确。我还刷新了数据库并尝试再次迁移,它给出了相同的结果。

最佳答案

您的 time_fromtime_to 默认值是错误的。您应该提供默认值而不是格式。如果您的模型是新的,请删除迁移文件(我认为dev_app.0016_auto_20180112_0630)更改默认值并再次运行manage.py makemigrations

关于python - Django 日期时间声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48258696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com