Opened 12 years ago

Closed 12 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

Description

forms.py

DATE_INPUT_FORMAT = (

('%d/%m/%Y','%m/%d/%Y')

)

class ReportDatetimeForm(forms.ModelForm):

manual_date = forms.DateField(input_formats = DATE_INPUT_FORMAT,

widget=forms.DateInput(attrs={'size':'15','id':'datepicker','readonly':'readonly'}))


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 forms.py to save the date in database in single format.

Thanks

Change History (1)

comment:1 by Baptiste Mispelon, 12 years ago

Resolution: invalid
Status: newclosed

Hi,

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].

Thanks.

[1] https://docs.djangoproject.com/en/1.5/faq/help/

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