8

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?

Andor
  • 1,998
  • 3
  • 16
  • 24

6 Answers6

11

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 to False, what you should do is specify the DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS in your settings.py. Here are the settings I use for this, based on converting the defaults:

#dd/mm/yyyy and dd/mm/yy date & datetime input field settings
DATE_INPUT_FORMATS = ('%d-%m-%Y', '%d/%m/%Y', '%d/%m/%y', '%d %b %Y',
                      '%d %b, %Y', '%d %b %Y', '%d %b, %Y', '%d %B, %Y',
                      '%d %B %Y')
DATETIME_INPUT_FORMATS = ('%d/%m/%Y %H:%M:%S', '%d/%m/%Y %H:%M', '%d/%m/%Y',
                          '%d/%m/%y %H:%M:%S', '%d/%m/%y %H:%M', '%d/%m/%y',
                          '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%Y-%m-%d')

If you have USE_L10N set to True, then you will need to use the FORMAT_MODULE_PATH instead.

For example, my LANGUAGE_CODE is set to en-au, my site is called golf, and my FORMAT_MODULE_PATH is set to golf.formats, so my directory structure looks like this:

golf/
    settings.py
    ...
    formats/
        __init__.py
        en/
            __init__.py
            formats.py

and the DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS settings are in formats.py instead of settings.py.

Caspar
  • 7,039
  • 4
  • 29
  • 41
5

I've modified settings so that it disables l10n and set date format explicite:

USE_L10N = False
DATE_FORMAT = 'd-m-Y'
DATETIME_FORMAT = 'd-m-Y H:i'

You can set DATE_INPUT_FORMATS as well.

alekwisnia
  • 2,314
  • 3
  • 24
  • 39
2

Based on this idea I made new db.fields class EuDateField:

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)

Note that it adds my format (e.g. 31.12.2007) to existing "standard" django formats at first place.

Usage:

from mydbfields import EuDateField
class Person(models.Model):
    ...
    birthday   = EuDateField("Birthday", null=True, blank=True, help_text="")

In my case this renders good in admin, but most probably will in ModelForm too (haven't tried it).

My django version is:

>>> import django
>>> django.get_version()
u'1.1 alpha 1 SVN-10105'
  • Ok, this solution is nice, but how can I use it with AdminDateWidget? This Widget is a part of my MultiWidget. – alekwisnia Dec 22 '11 at 11:54
2

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:

class MyDateField(forms.DateField):
  def __init__(self, *args, **kwargs):
    kwargs.setdefault('input_formats', ("%d-%m-%Y",))
    super(MyDateField, self).__init__(*args, **kwargs)

Note that input_formats is a list, so you can specify multiple possibilities and it will try them in order when parsing user input.

Carl Meyer
  • 122,012
  • 20
  • 106
  • 116
  • I can apply this on my new forms, but, how can I override the default form field in the admin site? – Andor Jun 05 '09 at 10:32
1

Looks like this is not yet supported, as I understand it, so yes, you'll have to do some custom work for now.

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
0

just beware not to write it as a string but as a tuple. eg:

DATE_INPUT_FORMATS = ('%d.%m.%Y',) 

if you want to have only one valid way of writing date in a form.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
ssbljk
  • 109
  • 1
  • 5