开发者

django autoconverting datetime property

开发者 https://www.devze.com 2023-02-17 03:56 出处:网络
my datetime propert开发者_如何转开发y is saving in mysql in this format 2011-03-17 00:00:00 but after fetchind the data with filter function it is giving March 17,2011 midnight but i have not say to d

my datetime propert开发者_如何转开发y is saving in mysql in this format 2011-03-17 00:00:00 but after fetchind the data with filter function it is giving March 17,2011 midnight but i have not say to do any this type of task. My question is how can i insist django to stic to show same value what is saved in MYSQL.


you'll want to use the datetime format, django's DateTimeField[1] really is a wrapper for datetime.datetime.

in the templates you can use the date[2] filter to apply the format you want for example:

{{ item.date|date:"Y-m-d H:i:s" }}

This should print out 2011-03-17 00:00:00 in the template. In views use datetimes.strftime[3]

[1] http://docs.djangoproject.com/en/dev/ref/models/fields/#datetimefield

[2] http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#date

[3] http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior


I have a feeling your database schema knows this is a date, right? In that case it's not being stored in the format you describe, but as some representation such as seconds since the era.

This means that when you retreave it your code has to do something with it to make it look right. If you don't tell it how to look it'll default to the format you see, but if you use strftime in your python code and a filter in your templates you can make it look however you like, including the original format how you saw it.

Of course the easy way out is to store it in the db as text...

0

精彩评论

暂无评论...
验证码 换一张
取 消