Django 查找数据库中最旧的条目
在 Django 视图中寻找一种方法来查找数据库中最旧的条目。然后取该值以及此后的某个时间。必须有一种简单的方法来通过编写复杂的查询来做到这一点。
Looking for a way in a Django view to find the oldest entry in the database. Then take that value and to a time since. There has to be a simple way to do this with writing a complicated query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的模型上有一个创建的时间戳,那么您可以按此字段升序排序并获取第一个值吗?
要计算日期之间的差异,您也许可以使用聚合函数来查找最高和最低日期,但我不确定。以下内容可能有效:
MyModel.objects.all().aggregate(lowest=Min('created_at'), Highest=Max('created_at'))
然后计算它们之间的差异两个 - 这当然取决于最小和最大聚合函数与日期的正确配合...
If you have a created timestamp on your model, then could you sort ascending by this field and grab the first value?
To do the differences between the dates, you may be able to use an aggregate function to find the highest and lowest date, but I am uncertain. Something along the following lines might work:
MyModel.objects.all().aggregate(lowest=Min('created_at'), highest=Max('created_at'))
And then calculate the difference between those two - this all depending of course on Min and Max aggregate functions working correctly with dates...