使Django查询本地化日期 [英] Making Django queries with localized dates

查看:151
本文介绍了使Django查询本地化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单中,我有一个名为booking_date的DateField,它使用AdminDateWidget呈现。 booking_date字段的内容需要国际化。当我想使用这样的字段的值时,出现问题:

In my form I have a DateField called booking_date that is rendered with the AdminDateWidget. The contents of the booking_date field needs to be internationalized. The problem appears when I want to use the value of the field in something like this:

booking = Booking.objects.get(booking_number='BN34D', booking_date='2010-11-21')

但是如果我的日期格式是'%d。%m。%Y'

But if my date format is '%d.%m.%Y':

booking = Booking.objects.get(booking_number='BN34D', booking_date='21.11.2010')

我得到一个'ValidationError:输入YYYY-MM-DD格式的有效日期'

I get a 'ValidationError: Enter a valid date in YYYY-MM-DD format'

无论使用何种日期格式,我如何进行查询?

How can I make the query regardless of the date format used?

推荐答案

我已经解决了这个问题:

I have solved the problem using this:

from django.utils import formats

formats.get_format('DATE_INPUT_FORMATS')[0]

然后,此格式用于解析日期,如 xyld显示

This format is then used for parsing the date like xyld showed.

这篇关于使Django查询本地化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆