On Tue, Aug 4, 2009 at 2:05 PM, zayatzz <[email protected]> wrote:
>
> Thanks everybody.
>
> I changed this :
> birth_date = forms.DateField(('%d/%m/%Y',), label='Birth Date',
> required=False ),
>
> for this :
> birth_date = forms.DateField(('%d/%m/%Y',), label='Birth Date',
> required=False, widget=forms.DateInput(format='%d/%m/%Y') )
>
> And it all works very good.
>
> But there's something else fishy here.
>
> If i tried this:
> birth_date = forms.DateField(label='Birth Date', required=False,
> widget=forms.DateInput(format='%d/%m/%Y') )
>
> Then it did not save in correct format again. But guess in which
> format did it save?
> %m/%d/%Y
>
> Why the hell though? could this be a minor bug in django code?
>
No, it's not a bug in Django. You removed the parameter that told the Field
how to properly interpret the date-value-as-string and turn it into a Python
date.
There are two format-type parameters.
The first, the one passed to the Field, tells the field what formats to try
when converting form a date-formatted-as-string (as will be present in POST
data) to a Python date object. This format parameter is a sequence, and the
formats in it are tried in order until Python is able to parse the string
into a date value (or, until the sequence of formats is exhausted, in which
case a ValidationError is raised).
The second, the one passed to the Widget, tells the Widget in what format to
output existing dates. This is not a list, it is just a single value: your
preferred way of formatting dates for output.
If you want things to work properly, you need to make sure that your Field
is going to properly recognize the format you have asked the Widget to
output. So your Field has to have the format you have specified for the
Widget in its list of allowed input formats. But when you removed the
('%d/%m/%Y',) parameter to the Field, you changed things so that the Field
will use the default list of input formats:
http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.DateField.input_formats
None of the formats in the default list are "%d/%m/%Y". Worse, one of the
default formats is "%m/%d/%Y" which will succeed in parsing some dates that
are actually in "%d/%m/%Y" format, only the values for day and month will
get swapped. Which is what you saw, I gather. The fix is to put back the
format parameter you had been passing to the DateField: it's necessary for
the format you are trying to use, as that is not a format that DateField
will be attempting to interpret dates with by default.
Karen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---