【问题标题】:Django DateTimeField ValidationError: value has an invalid formatDjango DateTimeField ValidationError:值的格式无效
【发布时间】:2016-09-20 01:23:19
【问题描述】:

尝试在 django DateTimeField 中存储日期时间时遇到验证错误。

下面是我要保存的格式以及错误。 :

django.core.exceptions.ValidationError: ["'Mon, 23 May 2016 08:30:15 GMT' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][T

在我的 django 设置中,我有:

DATE_INPUT_FORMATS = ("%d %b %Y", )

我应该在保存之前转换格式还是可以添加到DATE_INPUT_FORMATS

【问题讨论】:

  • DATE_INPUT_FORMATS 用于 DateField 而不是 DateTimeField

标签: python django django-models


【解决方案1】:

Django 参考:DateTimeField

这是答案:StackOverflow Answer Reference

【讨论】:

  • 我尝试使用>>> from django.utils.dateparse import parse_datetime >>> parse_datetime('Mon, 23 May 2016 08:30:15 GMT') 将其转换为有效格式,但这只会返回一个空白结果——奇怪的是没有错误吗?转换格式的最佳方法是什么?
  • >>> from datetime import datetimedate_object = datetime.strptime('Mon, 23 May 2016 08:30:15 GMT', '%a, %d %B %Y %H:%M:%S GMT') 这样试试。
  • 感谢,即使指定了 GMT,也会出现天真的日期时间错误?
  • 支持 GMT。我使用了该列表docs.python.org/2/library/…,它有效! datetime.strptime('Mon, 23 May 2016 08:30:15 GMT', '%a, %d %B %Y %H:%M:%S %Z') 是的,它来自标准 python 库。
  • 奇怪的是,我仍然收到天真的日期时间警告。解析似乎没有处理时区>>> datetime.strptime('Mon, 23 May 2016 08:30:15 GMT', '%a, %d %B %Y %H:%M:%S %Z') Out[7]: datetime.datetime(2016, 5, 23, 8, 30, 15)
猜你喜欢
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 2018-10-21
相关资源
最近更新 更多