[Answered ]-Grouping entries from PostgreSQL database by their week or month based on timestamp value?

1👍

You don’t want to use ExtractWeek because that just gets an integer value of the week in the year, e.g. 44, this can cause clashes across datasets that exceed a year.

What you will want to use is TruncWeek which essentially caps a data value at the Monday of that week. You can combine this with order_by() to get your data grouped together.

from django.db.models.functions import TruncWeek

weeks = EventItem.objects.annotate(week=TruncWeek('StartTime').order_by('week')
👤0sVoid

Leave a comment