Opened 6 years ago

Closed 6 years ago

#20616 closed Uncategorized (invalid)

save date in one format in Python/Django

Reported by: Raju Owned by: nobody
Component: Python 2 Version: 1.3
Severity: Normal Keywords: django 1.3, python 2.7
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no





class ReportDatetimeForm(forms.ModelForm):

manual_date = forms.DateField(input_formats = DATE_INPUT_FORMAT,


class Meta:

model = Report
fields = manual_date?

This is my form to get the input as date with two different format.While giving input date in this format %m/%d/%Y' the date and month gets interchange while save in database.That affects in my application.So i want to save both input format in a single format namely yyyy-mm-dd,so what ever input format the user enter ,the given format should save in the above mentioned format.

I want to know how to write a custom function in to save the date in database in single format.


Change History (1)

comment:1 Changed 6 years ago by Baptiste Mispelon

Resolution: invalid
Status: newclosed


Dates are saved as date objects in the database, so the format is irrelevant.
The format only matters when transforming a string into a date object (like in a form for example) and is not stored alongside the data.

The way Django works is that when given a bit of text, it iterates through the input formats you provided (in order) and tries to convert the text according to that format.
If the conversion succeeds, the corresponding date object is used and if it fails, it moves on to the next format.
If all formats fail, then a validation error is raised.

Using your example, both strings '18/6/2013' (matches the first format) and '6/18/2013' (matches the second format, but not the first) would get converted to the date of June 18th 2013.

Finally, note that this tracker is for reporting bugs against Django. For support questions, you should try the mailing list or the IRC channel instead [1].



Note: See TracTickets for help on using tickets.
Back to Top