Django default date formatting changes

5,174 views
Skip to first unread message

vishak

unread,
Oct 16, 2009, 6:21:33 AM10/16/09
to Django users
Hi Django users,

I have one question in django for which details are given below

1] The default date format in django is '`2009-10-10' when entered
through frontend (i.e by using models.DateField()). I manipulated
this behavior to '10-OCT-09'.

2] Everything is fine upto now, but when i save the data the django
validation error comes saying
"Date format should be YYYY-mm-dd".

Can anyone provide a solution to override this behavior of django and
also explain why django supports only 'yyyy-mm-dd' format.

Thanks in advance

Vishak.V

Михаил Лукин

unread,
Oct 16, 2009, 7:41:28 AM10/16/09
to django...@googlegroups.com

vishak

unread,
Oct 16, 2009, 7:44:22 AM10/16/09
to Django users
what you have told is OK but when you enter in datefield 10-OCT-09
instead of 2009-10-10 it gives error.

On Oct 16, 4:41 pm, Михаил Лукин <mihail.lu...@googlemail.com> wrote:
> Try thishttp://docs.djangoproject.com/en/dev/ref/settings/#date-format

Bayuadji

unread,
Oct 16, 2009, 7:56:51 AM10/16/09
to django...@googlegroups.com
Hi,

perhaps you could create clean_*date in your form,
and change the datefield 10-OCT-09 to 10-10-2009

-djibon-
--
--------------------------------------------------------------
http://www.tumbletooth.org
my linkedin profile : http://www.linkedin.com/in/bayuadji
--------------------------------------------------------------

David De La Harpe Golden

unread,
Oct 16, 2009, 9:07:04 AM10/16/09
to django...@googlegroups.com
vishak wrote:
> what you have told is OK but when you enter in datefield 10-OCT-09
> instead of 2009-10-10 it gives error.
>

If you don't tell a forms.DateField to expect 10-OCT-09, it won't.
Django's DateField default input format list doesn't include that
particular '%d-%b-%y' format by default.

It does have a bunch of others, including e.g. "10 Oct 2009", _not_ just
'yyyy-mm-dd'! Beware in particular it allows the confusing 'mm/dd/yyyy'
order that you might well want to kill with fire unless you're american.
http://docs.djangoproject.com/en/dev/ref/forms/fields/#datefield

You are probably autogenerating forms from your models, in your
frontend or in the django admin or both. If so, model.DateFields will
engender form.DateFields with default values including the default
input_formats list. So you need to override the form field
appropriately to set a nondefault input_formats list including
your desired one.

For your own forms on a one-off individual field basis, this is easy,
just override the field in your form class appropriately.

To do a bunch of 'em, one possible approach you could use is to make
datefield subclass with the default you want
http://docs.djangoproject.com/en/dev/howto/custom-model-fields/

class MyFormDateField(forms.DateField):
def __init__(self, *args, **kwargs):
# you still want %Y-%m-%d on the list as it's
# what the javascript datepicker used by the admin
# injects on the client side.
d = dict(input_formats=('%d-%b-%y','%Y-%m-%d',))
d.update(kwargs)
super(MyDateField, self).__init__(*args, **d)

class MyModelDateField(models.DateField):
def formfield(self, *args, **kwargs):
d = dict(form_class=MyFormDateField)
d.update(kwargs)
return super(MyModelDateField, self).formfield(*args, **d)

class MyModel(models.Model):
mydate = MyModelDateField()


Reply all
Reply to author
Forward
0 new messages