Код: Выделить всё
class Price:
timestamp = models.IntegerField()
price = models.FloatField()
Код: Выделить всё
queryset = (
Price.objects.annotate(timestamp_agg=Floor(F('timestamp') / 3600))
.values('timestamp_agg')
.annotate(
timestamp=Min('timestamp'),
high=Max('price'),
)
.values('timestamp', 'high')
.order_by('timestamp')
)
Код: Выделить всё
select min(timestamp) timestamp, max(price) high
from core_price
group by floor((timestamp / 3600))
order by timestamp
Код: Выделить всё
select *, avg(high) over (order by timestamp rows between 4 preceding and current row) ma
from (select min(timestamp) timestamp, max(price) high
from core_price
group by floor((timestamp / 3600))
order by timestamp)
Код: Выделить всё
Window(expression=Avg('price'), frame=RowRange(start=-4, end=0))
Код: Выделить всё
>>> queryset.annotate(ma=Window(expression=Avg('high'), frame=RowRange(start=-4, end=0)))
django.core.exceptions.FieldError: Cannot compute Avg('high'): 'high' is an aggregate
- django 5.2.7
- python 3.14
Подробнее здесь: https://stackoverflow.com/questions/797 ... -in-django