Is there any way I can get django to store my data in postgresql as 'dd-mm-yyyy' (if required) and have the django forms validate for 'dd-mm-yyyy'?
有什么方法可以让django将我的数据存储在postgresql中作为'dd-mm-yyyy'(如果需要)并让django表单验证'dd-mm-yyyy'?
(I have tried without much success things like:
(我尝试过没有太多成功的事情:
DATE_INPUT_FORMATS = ('%d-%m-%Y')
USE_I18N = True
USE_L10N = True
And done a lot of googling but no success :(
并做了很多谷歌搜索,但没有成功:(
6 个解决方案
#1
29
The problem turned out to be that I needed both the ISO and UK date formats in the settings.py
file like so:
结果问题是我在settings.py文件中需要ISO和UK日期格式,如下所示:
DATE_INPUT_FORMATS = ('%d-%m-%Y','%Y-%m-%d')
and then the forms.py adjusted to the following:
然后将forms.py调整为以下内容:
class ClientDetailsForm(ModelForm):
date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
class Meta:
model = ClientDetails
Why USE_L10N = True
can't just do this I don't know!
为什么USE_L10N = True不能这样做我不知道!
[感谢这个和这个]
#2
18
DATE_INPUT_FORMATS
in settings.py has no effect with USE_I18N = True
. This is because django will load specific format for active locale. Django ships with format definitions for many different locales.
settings.py中的DATE_INPUT_FORMATS对USE_I18N = True无效。这是因为django将为活动区域设置加载特定格式。 Django附带了许多不同语言环境的格式定义。
You can override django default format definitions:
您可以覆盖django默认格式定义:
mysite/
formats/
__init__.py
en/
__init__.py
formats.py
As described in django documentation: Creating custom formats file
如django文档中所述:创建自定义格式文件
#3
9
In Sevenearths answer there's the comment
在Sevenearths回答有评论
Why
USE_L10N = True
can't just do this I don't know!为什么USE_L10N = True不能这样做我不知道!
The documentation says that USE_L10N = True
is the default setting, but to get UK dates to work the LANGUAGE_CODE
must also be changed from its default setting of en-us
to en-GB
.
文档说明USE_L10N = True是默认设置,但要使UK日期生效,LANGUAGE_CODE也必须从其默认设置en-us更改为en-GB。
Once changed, date fields automatically accept the formats "dd/mm/yyyy" and "yyyy-mm-dd" (and possibly others, I've not tested them all) without needing to set input_formats = settings.DATE_INPUT_FORMATS
in every form date field.
更改后,日期字段会自动接受格式“dd / mm / yyyy”和“yyyy-mm-dd”(可能还有其他格式,我没有全部测试过),无需在每个表单日期设置input_formats = settings.DATE_INPUT_FORMATS领域。
#4
2
This might not be the answer to the exact question, but since I got here... I would suggest using a date widget, and let Django save the date the way it understands it. Define the field in your form like so:
这可能不是确切问题的答案,但是因为我到了这里......我建议使用日期小部件,让Django以它理解的方式保存日期。在表单中定义字段,如下所示:
import datetime
from django import forms
def last_years():
first_year = datetime.datetime.now().year - 6
return list(range(first_year + 7, first_year, -1))
class MyForm(forms.Form):
# This is it... it's user friendly, and can't go wrong parsing it
date = forms.DateField(widget=forms.SelectDateWidget(years = last_years()))
If you got it into a date field of the Django Model, then the only problem left would be format the output representation. Am I right?
如果你把它放到了Django Model的日期字段中,那么剩下的唯一问题就是格式化输出表示。我对吗?
#5
0
I would overwrite the DateTimeField put my our logic. It doesn't matter how postgresql is storing it, you can format it how ever you want.
我会覆盖DateTimeField把我的逻辑。 postgresql如何存储它并不重要,您可以根据需要对其进行格式化。
Docs: http://docs.djangoproject.com/en/dev/ref/models/instances/
文档:http://docs.djangoproject.com/en/dev/ref/models/instances/
#6
-2
You could use a CharField if you wanted to store it in exactly this format, otherwise it will use the python datetime format.
如果你想以这种格式存储它,你可以使用CharField,否则它将使用python datetime格式。
If you use a CharField, you can then write a form with custom validation for this field.
如果使用CharField,则可以编写包含此字段的自定义验证的表单。
#1
29
The problem turned out to be that I needed both the ISO and UK date formats in the settings.py
file like so:
结果问题是我在settings.py文件中需要ISO和UK日期格式,如下所示:
DATE_INPUT_FORMATS = ('%d-%m-%Y','%Y-%m-%d')
and then the forms.py adjusted to the following:
然后将forms.py调整为以下内容:
class ClientDetailsForm(ModelForm):
date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
class Meta:
model = ClientDetails
Why USE_L10N = True
can't just do this I don't know!
为什么USE_L10N = True不能这样做我不知道!
[感谢这个和这个]
#2
18
DATE_INPUT_FORMATS
in settings.py has no effect with USE_I18N = True
. This is because django will load specific format for active locale. Django ships with format definitions for many different locales.
settings.py中的DATE_INPUT_FORMATS对USE_I18N = True无效。这是因为django将为活动区域设置加载特定格式。 Django附带了许多不同语言环境的格式定义。
You can override django default format definitions:
您可以覆盖django默认格式定义:
mysite/
formats/
__init__.py
en/
__init__.py
formats.py
As described in django documentation: Creating custom formats file
如django文档中所述:创建自定义格式文件
#3
9
In Sevenearths answer there's the comment
在Sevenearths回答有评论
Why
USE_L10N = True
can't just do this I don't know!为什么USE_L10N = True不能这样做我不知道!
The documentation says that USE_L10N = True
is the default setting, but to get UK dates to work the LANGUAGE_CODE
must also be changed from its default setting of en-us
to en-GB
.
文档说明USE_L10N = True是默认设置,但要使UK日期生效,LANGUAGE_CODE也必须从其默认设置en-us更改为en-GB。
Once changed, date fields automatically accept the formats "dd/mm/yyyy" and "yyyy-mm-dd" (and possibly others, I've not tested them all) without needing to set input_formats = settings.DATE_INPUT_FORMATS
in every form date field.
更改后,日期字段会自动接受格式“dd / mm / yyyy”和“yyyy-mm-dd”(可能还有其他格式,我没有全部测试过),无需在每个表单日期设置input_formats = settings.DATE_INPUT_FORMATS领域。
#4
2
This might not be the answer to the exact question, but since I got here... I would suggest using a date widget, and let Django save the date the way it understands it. Define the field in your form like so:
这可能不是确切问题的答案,但是因为我到了这里......我建议使用日期小部件,让Django以它理解的方式保存日期。在表单中定义字段,如下所示:
import datetime
from django import forms
def last_years():
first_year = datetime.datetime.now().year - 6
return list(range(first_year + 7, first_year, -1))
class MyForm(forms.Form):
# This is it... it's user friendly, and can't go wrong parsing it
date = forms.DateField(widget=forms.SelectDateWidget(years = last_years()))
If you got it into a date field of the Django Model, then the only problem left would be format the output representation. Am I right?
如果你把它放到了Django Model的日期字段中,那么剩下的唯一问题就是格式化输出表示。我对吗?
#5
0
I would overwrite the DateTimeField put my our logic. It doesn't matter how postgresql is storing it, you can format it how ever you want.
我会覆盖DateTimeField把我的逻辑。 postgresql如何存储它并不重要,您可以根据需要对其进行格式化。
Docs: http://docs.djangoproject.com/en/dev/ref/models/instances/
文档:http://docs.djangoproject.com/en/dev/ref/models/instances/
#6
-2
You could use a CharField if you wanted to store it in exactly this format, otherwise it will use the python datetime format.
如果你想以这种格式存储它,你可以使用CharField,否则它将使用python datetime格式。
If you use a CharField, you can then write a form with custom validation for this field.
如果使用CharField,则可以编写包含此字段的自定义验证的表单。