Models.DateField格式问题 [英] Models.DateField Format issues

查看:486
本文介绍了Models.DateField格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,它有一个日期字段

I have a model, which has a date field

date_of_birth = models.DateField(blank=True, null=True, verbose_name="DOB")

我想格式化为格式 dd / MM / yyyy ,但我尝试的一切都失败了。

I would like to format it to save dates in the format dd/MM/yyyy, but everything I have tried fails.

我认为默认值必须是 YYYY-MM-dd 因为这是它如何保存到我的数据库。尝试以不同的格式提交日期会出现错误:

I think the default must be YYYY-MM-dd because that is how it saves to my database. Trying to submit dates in a different format gives the error:

[u"'17/01/1970' value has an invalid date format. It must be in YYYY-MM-DD format."]

一个href =https://docs.djangoproject.com/en/1.7/ref/forms/widgets/ =nofollow>日期小部件,但有几个问题让它与我的兼容models.py

I have tried using Date Widgets but am having a few issues getting it to be compatible with my models.py

推荐答案

您可以通过覆盖 input_formats 您的表单中的 DateField 这在文档中有很好的涵义。默认日期格式为

You can change this by overriding input_formats on the DateField in your form. This is covered pretty well in the documentation. The default date formats are

['%Y-%m-%d',      # '2006-10-25'
'%m/%d/%Y',       # '10/25/2006'
'%m/%d/%y']       # '10/25/06'

['%Y-%m-%d',      # '2006-10-25'
'%m/%d/%Y',       # '10/25/2006'
'%m/%d/%y',       # '10/25/06'
'%b %d %Y',      # 'Oct 25 2006'
'%b %d, %Y',      # 'Oct 25, 2006'
'%d %b %Y',       # '25 Oct 2006'
'%d %b, %Y',      # '25 Oct, 2006'
'%B %d %Y',       # 'October 25 2006'
'%B %d, %Y',      # 'October 25, 2006'
'%d %B %Y',       # '25 October 2006'
'%d %B, %Y']      # '25 October, 2006'

根据您是否在您的设置中使用本地化。

Depending on whether or not you are using localization in your settings.

您似乎正在寻找%d /%m / %y ,不是默认格式。

You appear to be looking for %d/%m/%y, which isn't a default format.

这篇关于Models.DateField格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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