DateField with Django

1,498 views
Skip to first unread message

Edgard Matos

unread,
Feb 11, 2009, 9:36:06 AM2/11/09
to django...@googlegroups.com
Hi!

I have a model with a DateField and I'm setting the input_formats properties to "%d/%m/%Y":
birth_date = forms.DateField(input_formats=("%d/%m/%Y",))

When I go add a object in this format ("%Y/%m/%d"), the django validation validate with sucess.

But when I edit my object:
form = UserForm(instance=stored_user)
Django fill the DateField in this format: ("%Y-%m-%d")

How I do to Django fill the field in this format ("%Y/%m/%d") ?

Anybody help me?
Thanks!

Edgard Matos from Brazil

Edgard Matos

unread,
Feb 11, 2009, 12:19:18 PM2/11/09
to django...@googlegroups.com
Anybody help me?
--
Atenciosamente,
Edgard Matos
E-mail: edgar...@gmail.com
Skype: edgardmatos
Celular: 85 8837 8285

Alex Gaynor

unread,
Feb 11, 2009, 1:21:30 PM2/11/09
to django...@googlegroups.com
Please be more patient, you've scarcely waited 3 hours before sending another email.  Remember that everyone who replies here is a volunteer, and we are scattered around the world, across many time zones, so try to wait at least a full day before "bumping" a thread.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Edgard Matos

unread,
Feb 12, 2009, 6:15:50 AM2/12/09
to django...@googlegroups.com
You have reason Alex.

I'm sorry.

Wim Feijen

unread,
Mar 18, 2009, 6:51:13 AM3/18/09
to Django users
Hello people,

It has been over a month do I'd like to raise the same question again,
with all due respect.

Does anybody know how to control a form's output format of a stored
DateField? Unfortunately, different countries use different customs to
represent dates and I'd like to represent the date in another
international format (%d-%m-%Y). Any help would be very much
appreciated!

A major thanks,

Wim

On Feb 12, 12:15 pm, Edgard Matos <edgardma...@gmail.com> wrote:
> You have reason Alex.
>
> I'm sorry.
>
>
>
> On Wed, Feb 11, 2009 at 3:21 PM, Alex Gaynor <alex.gay...@gmail.com> wrote:
>
> > On Wed, Feb 11, 2009 at 12:19 PM, Edgard Matos <edgardma...@gmail.com>wrote:
>
> >> Anybody help me?
>
> >> On Wed, Feb 11, 2009 at 11:36 AM, Edgard Matos <edgardma...@gmail.com>wrote:
>
> >>> Hi!
>
> >>> I have a model with a DateField and I'm setting the input_formats
> >>> properties to "%d/%m/%Y":
> >>> birth_date = forms.DateField(input_formats=("%d/%m/%Y",))
>
> >>> When I go add a object in this format ("%Y/%m/%d"), the django validation
> >>> validate with sucess.
>
> >>> But when I edit my object:
> >>> form = UserForm(instance=stored_user)
> >>> Django fill the DateField in this format: ("%Y-%m-%d")
>
> >>> How I do to Django fill the field in this format ("%Y/%m/%d") ?
>
> >>> Anybody help me?
> >>> Thanks!
>
> >>> Edgard Matos from Brazil
>
> >> --
> >> Atenciosamente,
> >> Edgard Matos
> >> E-mail: edgardma...@gmail.com
> >> Skype: edgardmatos
> >> Celular: 85 8837 8285
>
> > Please be more patient, you've scarcely waited 3 hours before sending
> > another email.  Remember that everyone who replies here is a volunteer, and
> > we are scattered around the world, across many time zones, so try to wait at
> > least a full day before "bumping" a thread.
>
> > Alex
>
> > --
> > "I disapprove of what you say, but I will defend to the death your right to
> > say it." --Voltaire
> > "The people's good is the highest law."--Cicero
>
> --
> Atenciosamente,
> Edgard Matos
> E-mail: edgardma...@gmail.com

koenb

unread,
Mar 18, 2009, 8:28:22 AM3/18/09
to Django users
> Does anybody know how to control a form's output format of a stored
> DateField? Unfortunately, different countries use different customs to
> represent dates and I'd like to represent the date in another
> international format (%d-%m-%Y). Any help would be very much
> appreciated!

I use the following combination of formfield and widget:

as formfield:

class DateFormField(forms.DateField):
def __init__(self, input_formats=None, *args, **kwargs):
super(DateFormField, self).__init__(*args, **kwargs)
self.input_formats = input_formats or
settings.DATE_INPUT_FORMATS

def clean(self, value):
super(forms.DateField, self).clean(value)
if value in forms.fields.EMPTY_VALUES:
return None
if isinstance(value, datetime.datetime):
return value.date()
if isinstance(value, datetime.date):
return value
for format in self.input_formats:
try:
y, m, d = time.strptime(value, format)[:3]
if y == 1900:
y = datetime.datetime.now().year
return datetime.date(y, m, d)
except ValueError:
continue
raise forms.util.ValidationError(self.error_messages
['invalid'])

note that I defined the input formats in my settings as

DATE_INPUT_FORMATS = (
'%d/%m/%Y', '%d/%m/%y', # '25/10/2006', '25/10/06'
'%d/%m', # '25/10'
'%Y-%m-%d', # '2006-10-25'
)

subclassing clean() allows me to enter dates without a year specified.
If you don't need this you can leave out the clean method definition.


and as widget:

class DateWidget(forms.DateTimeInput):
def __init__(self, attrs={}):
super(DateWidget, self).__init__
(format=settings.DATE_OUTPUT_FORMAT)

where my setting for the output format is:

DATE_OUTPUT_FORMAT = '%d/%m/%Y'

(to be honest my real widget also defines some media to use the jquery
datepicker)

Maybe this helps somebody,

cheers,

Koen

Adi Sieker

unread,
Mar 18, 2009, 8:48:21 AM3/18/09
to django...@googlegroups.com
Hi,

On 18.03.2009, at 11:51, Wim Feijen wrote:


Hello people,

It has been over a month do I'd like to raise the same question again,
with all due respect.

Does anybody know how to control a form's output format of a stored
DateField? Unfortunately, different countries use different customs to
represent dates and I'd like to represent the date in another
international format (%d-%m-%Y). Any help would be very much
appreciated!

A major thanks,


adi

--
Adi J. Sieker         mobile: +49 - 178 - 88 5 88 13
Freelance developer   skype:  adijsieker
SAP-Consultant        web:    http://www.sieker.info/profile
                      openbc: https://www.openbc.com/hp/AdiJoerg_Sieker/



Reply all
Reply to author
Forward
0 new messages