【发布时间】:2020-01-19 14:47:44
【问题描述】:
我正在对接我的 Django 应用程序。由于我将数据库从 SQLite 更改为 Postgres,因此迁移失败并出现此错误:
RuntimeWarning: DateTimeField Posts.created_at received a naive datetime (2020-01-19 14:26:30.893128) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.CannotCoerce: cannot cast type time without time zone to timestamp with time zone
LINE 1: ..." TYPE timestamp with time zone USING "created_at"::timestam...
我已经搜索了其他问题并尝试了此修复https://stackoverflow.com/a/20106079/7400518
我使用的是datetime.now(),然后我将其更改为timezone.now(),但我仍然遇到同样的错误。
这是来自models.py的相关行
from django.utils import timezone
class Posts(models.Model):
created_at = models.DateTimeField(default=timezone.now(), blank=True)
【问题讨论】:
-
删除仍使用旧
datetime的迁移文件。此外,您应该使用auto_now_add。
标签: python django postgresql docker