python - How can I convert the resulting date format to another in django? -


i creating app in django, , have next problem:

i have model has attribute "date", , save date in format: yyyy-mm-dd.

but when instance, , date attribute value of instance, date is: "oct. 10, 2015", error:

[u"'oct.' value has invalid date format. must in yyyy-mm-dd format."] 

how can solve error? there way format "oct. 10, 2015" yyyy-mm-dd?

thank much!

for input oct. 10, 2015, need write this:

from datetime import datetime datetime.strptime('oct. 10, 2015', '%b. %d, %y') 

django's datefield saves datetime object directly, convert input string required format.

you can try input data below using form:

class someform(forms.modelform):     date_input_field = forms.charfield()      def clean_date_input_field(self):         try:              return datetime.strptime(self.cleaned_data['date_input_field'], '%d/%m/%y')         except:              return datetime.strptime(self.cleaned_data['date_input_field'], '%b. %d, %y')      class meta:           ... 

or add '%b. %d, %y' in input_format.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -