试图更改存储日期时间模型的方式(Trying to change the way date time model is stored)

编程入门 行业动态 更新时间:2024-10-25 09:38:26
试图更改存储日期时间模型的方式(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.

更多推荐

本文发布于:2023-08-07 21:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   日期   方式   时间   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!