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.
The default for the admin is: YYYY-MM-DD
But would be awesome to use: DD-MM-YYYY
Is this integrated in any case in i18n? Can this be changed without a custom model?
Based on this idea I made new db.fields class EuDateField:
mydbfields.py
Note that it adds my format (e.g. 31.12.2007) to existing "standard" django formats at first place.
Usage:
In my case this renders good in admin, but most probably will in ModelForm too (haven't tried it).
My django version is:
You have to do this yourself for now, but it's quite easy to do with a custom form field class that sets the input_formats argument of DateField. This should do it:
Note that input_formats is a list, so you can specify multiple possibilities and it will try them in order when parsing user input.
Looks like this is not yet supported, as I understand it, so yes, you'll have to do some custom work for now.
There is an official way to do this now since the closing of Django ticket 6483 & release of Django 1.2.
If you have
USE_L10N
set toFalse
, what you should do is specify theDATE_INPUT_FORMATS
andDATETIME_INPUT_FORMATS
in yoursettings.py
. Here are the settings I use for this, based on converting the defaults:If you have
USE_L10N
set toTrue
, then you will need to use theFORMAT_MODULE_PATH
instead.For example, my
LANGUAGE_CODE
is set toen-au
, my site is calledgolf
, and myFORMAT_MODULE_PATH
is set togolf.formats
, so my directory structure looks like this:and the
DATE_INPUT_FORMATS
andDATETIME_INPUT_FORMATS
settings are informats.py
instead ofsettings.py
.I've modified settings so that it disables l10n and set date format explicite:
You can set DATE_INPUT_FORMATS as well.
just beware not to write it as a string but as a tuple. eg:
if you want to have only one valid way of writing date in a form.