问题描述
Django有一个DATE_FORMAT和DATE_TIME_FORMAT选项,让我们可以选择在查看日期时使用哪种格式,但并不显然让我更改在Django Admin中编辑或添加的日期的输入格式。
Django has a DATE_FORMAT and a DATE_TIME_FORMAT options that allow us to choose which format to use when viewing dates, but doesn't apparently let me change the input format for the date when editing or adding in the Django Admin.
管理员的默认值为:YYYY-MM-DD
The default for the admin is: YYYY-MM-DD
但使用起来非常棒:DD-MM- YYYY
But would be awesome to use: DD-MM-YYYY
这是否在i18n中集成?
可以在没有自定义模型的情况下进行更改吗?
Is this integrated in any case in i18n?Can this be changed without a custom model?
推荐答案
根据这个想法,我创建了新的db.fields类EuDateField :
Based on this idea I made new db.fields class EuDateField:
mydbfields.py
mydbfields.py
from django import forms
from django.forms.fields import DEFAULT_DATE_INPUT_FORMATS
from django.db import models
class EuDateFormField(forms.DateField):
def __init__(self, *args, **kwargs):
kwargs.update({'input_formats': ("%d.%m.%Y",)+DEFAULT_DATE_INPUT_FORMATS})
super(EuDateFormField, self).__init__(*args, **kwargs)
class EuDateField(models.DateField):
def formfield(self, **kwargs):
kwargs.update({'form_class': EuDateFormField})
return super(EuDateField, self).formfield(**kwargs)
请注意,它添加了格式(例如31.12.2007)到现有的标准django格式。
Note that it adds my format (e.g. 31.12.2007) to existing "standard" django formats at first place.
用法:
from mydbfields import EuDateField
class Person(models.Model):
...
birthday = EuDateField("Birthday", null=True, blank=True, help_text="")
在我的情况下,这在管理员中表现不错,但最有可能在ModelForm中(没有
In my case this renders good in admin, but most probably will in ModelForm too (haven't tried it).
我的django版本是:
My django version is:
>>> import django
>>> django.get_version()
u'1.1 alpha 1 SVN-10105'
这篇关于Django Admin中的欧洲日期输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!