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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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…