试图更改存储日期时间模型的方式(Trying to change the way date time model is stored)
我刚刚开始使用Django和Python,现在我存储发布日期的方式是这样的。
models.py pub_date = models.DateTimeField('Date Published') views.py def home(request): today = Premium.objects.latest('pub_date') todays_date = today.pub_date.date() etc...我需要的日期是2015年4月3日格式,这就是为什么我这样做。 我想知道是否有办法改变模型和事物存储到数据库的方式,这样我每次想要调用日期时都不必运行pub_date.date()。
I'm just starting out with Django and Python and right now the way I store the date published is like this.
models.py pub_date = models.DateTimeField('Date Published') views.py def home(request): today = Premium.objects.latest('pub_date') todays_date = today.pub_date.date() etc...I need the date to be April 3, 2015 format so thats why I go about it like that. Im wondering if there is a way to change the model and the way things are stored into the DB so that I dont have to run a pub_date.date() every time I want to call the date.
最满意答案
如果您只想存储日期,则不应使用DateTimeField 。 请改用DateField 。
You shouldn't be using DateTimeField if you just want to store the date. Use DateField instead.
更多推荐
发布评论