Django group by jalali week number

178 Views Asked by At

I'm working on django api using a PostgreSQL database. The model has a date field representing a travel time (in jalali format) and an integer field representing number of passengers for that travel.

I'm using django_jalali to handle jalali date queries.

from django_jalali.db import models as jmodels

class TravelReport( models.Model ):

   travel_date = jmodels.jDateField(verbose_name="تاریخ سفر")
   passengers = models.IntegerField()

I want to aggregate sum of passengers for each week number in a jalali year. This is the query I'm using:

queryset= TravelReport.objects.annotate(week=F("travel_date__week")).values("week").annotate(total_passengers=Sum("passengers"))

Here is my problem: Django lookup __week is returning Georgian week number not jalali week number. For example week number for a travel date with jdatetime.date(1390, 5, 12) should be 19 but my query is returning 31.

What are my options to fix this?

1

There are 1 best solutions below

1
On

Ok. Lets work out options:

  1. override the lookup method to work using the jalali calender format.
  2. convert your dates to gregorian format and store in another field, and call a function taht takes as input jalali format date, and sends the reciprocating gregorian format date to the lookup field.
  3. Keep the number of passengers for a week stored in a separate field, with key as jalali calender weekname and value will be the number of passengers for that week.