Django find oldest entry in database
Looking for a way in a Django vi开发者_如何学Goew 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.
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...
精彩评论