Skip to content

Instantly share code, notes, and snippets.

@repodevs
Created January 27, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save repodevs/b9fd93987832b3b43296 to your computer and use it in GitHub Desktop.
Save repodevs/b9fd93987832b3b43296 to your computer and use it in GitHub Desktop.
models grouping by date and count data
# simple snippet data to grouping and counting a django models
# for example in this data
# I have fields with attribute DateTimeField
# so I use `Model.objects.datetimes`
# Because I just want get day,month,year
# If you just use fields `DateTime` Prefer you can use `Model.objects.date`
users = Model.objects.all() # Models to be grouping
count_user = [{'val': users.filter(date_joined__day=x.day,date_joined__month=x.month,date_joined__year=x.year).count(), 'date':x.strftime('%Y-%m-%d')} for x in Model.objects.datetimes('date_joined','day')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment