WARNINGS.WARN(“ DateTimeField%s)收到了幼稚的DateTime(%S)”

发布于 2025-01-21 09:07:27 字数 486 浏览 2 评论 0原文

我使用django简单历史来获取模型的历史 然后,我按日期搜索历史记录结果,但下面会出现错误。如何格式化日期?

RuntimeWarning: DateTimeField HistoricalIssue.history_date received a naive datetime (2022-04-13 10:34:32) while time zone support is active.
  warnings.warn("DateTimeField %s received a naive datetime (%s)"



def SearchByDate(request):
        date_search = request.POST['date-search']
        if date_search:
            admin_hist_search_results = Issue.history.filter(history_date=date_search)

I use django simple-history to get history on my models
I then search the history results by date but I get the error below. How can I format the date?

RuntimeWarning: DateTimeField HistoricalIssue.history_date received a naive datetime (2022-04-13 10:34:32) while time zone support is active.
  warnings.warn("DateTimeField %s received a naive datetime (%s)"



def SearchByDate(request):
        date_search = request.POST['date-search']
        if date_search:
            admin_hist_search_results = Issue.history.filter(history_date=date_search)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所有深爱都是秘密 2025-01-28 09:07:27

首先,请记住,这不是错误,而是“只”警告。它提到传入的时间戳(您将其存储在变量date_search中)没有时区信息,而您将其与时间戳字段进行比较(history_date_date on Model 问题)确实具有时区信息。这可能会导致问题。

如果您知道来自请求的时区,则可以将该信息添加到时间戳,例如:

import pytz

date_as_string = request.POST['date-search']
parsed_date = datetime.strptime(date_as_string, '%Y-%m-%d')
amsterdam_timezone = pytz.timezone('Europe/Amsterdam')
date_search = amsterdam_timezone.localize(parsed_date)

First, keep in mind that this is not an error, but "only" a warning. It mentions that the incoming timestamp (which you store in variable date_search) does not have timezone information, while you compare it with a timestamp field (history_date on model Issue) that does have timezone information. This could potentially lead to issues.

If you know the timezone coming in from the request, you can add that information to the timestamp, for example:

import pytz

date_as_string = request.POST['date-search']
parsed_date = datetime.strptime(date_as_string, '%Y-%m-%d')
amsterdam_timezone = pytz.timezone('Europe/Amsterdam')
date_search = amsterdam_timezone.localize(parsed_date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文