本文介紹了如何從模型中在 django 中設置 DateField 格式?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在創建一個 django 應用程序,但我遇到了下一個問題:當我想設置日期時會顯示此錯誤:
I am creating a django application and I have the next problem: this error is shown when I want to set a date:
ValidationError [u"'12/06/2012' value has an invalid date format. It must be in YYYY-MM-DD format."]
對于這個模型:
class ModelA(models.Model):
date1 = models.DateField(null=True)
date2 = models.DateField(null=True)
如何將 DateField 格式設置為 %m/%d/%Y
.
How can I set the DateField format to be %m/%d/%Y
.
選項input_formats"
無法識別.
謝謝!
推薦答案
正如@bruno 在他的回答中提到的, input_formats
是一個表單字段,但是它可以用來控制保存的日期格式來自模型.
As @bruno as mentioned in his answer, input_formats
is a forms field, however it can be used to control the date format saved from the model.
在 settings.py
中設置 DATE_INPUT_FORMATS
如下:
In settings.py
set DATE_INPUT_FORMATS
as below:
DATE_INPUT_FORMATS = ['%d-%m-%Y']
在您的表單中,您可以執行以下操作:
And in your form you could do something like below:
class ClientDetailsForm(ModelForm):
date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
class Meta:
model = ModelA
這篇關于如何從模型中在 django 中設置 DateField 格式?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!