Reputation: 3955
With a model like this:
class User(models.Model):
entered = models.DateTimeField(auto_now_add=True)
How to grab stats data so it show recent added users for last 30 days with number of users added on that day.
Example:
2010-08-02: 220
2010-08-08: 1220
2010-08-14: 20
Upvotes: 1
Views: 132
Reputation: 74705
Update:
This answer is not useful. As entered
is a DateTimeField
the query will end up with a count of 1 for each value of entered
. This is not what the OP wanted. (Thanks dannyroa)
Changing the type of entered
to DateField
should help.
Original Answer
You can use Django's aggregation feature to do this. Something like this:
from django.db.models import Count
q = User.objects.annotate(Count('entered')).order_by('-entered')
for user in q[0:30]:
print "%s %s" % (user.entered, user.entered__count)
Update
You can slice the queryset and then pass it into the template.
# View
def my_view(*args, **kwargs):
queryset = User.objects.annotate(Count('entered')).order_by('-entered')
context = dict(queryset = queryset[0:30])
return render_to_response('my_template.html', context_instance = RequestContext(request, context))
# Template
{% for user in queryset %}
<td>{{ user.entered }}</td>
<td>{{ user.entered_count }}</td>
{% endfor %}
Upvotes: 3