With a model like this in Django, how to retrive 30 days entries with count how many were added on that day.
class Entry(models.Model):
...
entered = models.DateTimeField(auto_now_add=True)
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.
Getting the last 30 days entries is easy:
Counting how many on each day is much trickier, because you’re storing timestamps, not days – if you were using a DateField you could just do a simple aggregation. Perhaps the best thing to do is iterate through and count them per day:
Now
countsis a dictionary whose keys are the number of days before today, and the values are the number of entries on that day.