Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting dates for annotating count in Django + Python 3

I'm currently trying to annotate and count some dates, based on the number of times they appear.

visits = Subs.objects.filter(camp=campdata, timestamp__lte=datetime.datetime.today(), timestamp__gt=datetime.datetime.today()-datetime.timedelta(days=30)).\
values('timestamp').annotate(count=Count('timestamp'))

If I print this in a for loop like,

for a in visits:
  print(a)

I would get back the following in Json.

{'timestamp': datetime.datetime(2018, 10, 5, 15, 16, 25, 130966, tzinfo=<UTC>), 'count': 1}
{'timestamp': datetime.datetime(2018, 10, 5, 15, 16, 45, 639464, tzinfo=<UTC>), 'count': 1}
{'timestamp': datetime.datetime(2018, 10, 6, 8, 43, 24, 721050, tzinfo=<UTC>), 'count': 1}
{'timestamp': datetime.datetime(2018, 10, 7, 4, 54, 59, tzinfo=<UTC>), 'count': 1}

This is kinda the right direction, however, it's counting to the second.. I just need to days, so that the event that happened on 2018, 10, 5 would be count: 2 for example.

Can anyone lead me into the right direction?

Additionally, whats the most "django" way of converting the dates into something more json / api friendly?

My ideal json return would be something like

{'timestamp': 2018-10-5, 'count': 2}

Thanks!

like image 271
Ronald Langeveld Avatar asked Oct 07 '18 09:10

Ronald Langeveld


People also ask

How to work with dates in Django?

Working with dates in Django? The datetime module of Python offers classes for manipulating dates and times easily. You can format any date of Python (as long as it is a datetime object) using the strftime method.

What is formatting in Django and how to enable it?

See Translation for more details. When formatting is enabled, Django can use localized formats when parsing dates, times and numbers in forms. That means it tries different formats for different locales when guessing the format used by the user when inputting data on forms.

What is annotation in Django with example?

What is annotation in Django? In general terms, annotations can be defined as commenting or adding notes with appropriate message/text. In Django, annotations are used to add additional columns to queryset objects while querying.

How to format date and time in Python?

The datetime module of Python offers classes for manipulating dates and times easily. You can format any date of Python (as long as it is a datetime object) using the strftime method. A datetime object will have the method strftime that allows you to provide a custom format to a date and print it as a string wherever you need to.


1 Answers

You can use the TruncDate annotation to achieve this:

visits = Subs.objects.annotate(date=TruncDate('timestamp')).filter(
    camp=campdata, 
    date__lte=datetime.datetime.today(), 
    date__gt=datetime.datetime.today() - datetime.timedelta(days=30)
).values('date').annotate(count=Count('date'))

As for your question about serializing dates for JSON, Django provides the DjangoJSONEncoder to help with just that:

import json
from django.core.serializers.json import DjangoJSONEncoder

json.dumps(list(visits), cls=DjangoJSONEncoder)
like image 173
solarissmoke Avatar answered Oct 13 '22 00:10

solarissmoke