Access Postgres generate_series function via Django model syntax

Viewed 21

I need to use Django Models to create roughly the following SQL:

SELECT series AT TIME ZONE 'utc' AS forecast_hour,
       app_forecasts.inventory AS inventory
  FROM generate_series(DATE_TRUNC('hour', TIMESTAMP '2022-05-01 04:00:00+00:00'), DATE_TRUNC('hour', TIMESTAMP '2022-09-03 03:00:00+00:00'), '1 hour'::interval) series
  LEFT JOIN app_forecasts
    ON series = DATE_TRUNC('hour', app_forecasts.forecast_at) AT TIME ZONE 'utc'

It would look something like:

Forecast.objects.annotate(dates=Func(..., ..., Value('1 day'), function='generate_series'))
0 Answers
Related