Django преобразование запроса postgresql с массивом и подзапросом в ORM
Проблема в том, что я не могу преобразовать этот запрос в Django ORM
select weekday,
(array(
select termdelay
from call_schedule_history c_in
where c_in.is_active
and c.weekday = c_in.weekday
order by 1
))
from call_schedule_history c
where is_active
group by 1, 2
Я пытался:
history_in = History.objects.filter(weekday=OuterRef('pk'))
history_out = History.objects.values('weekday').annotate(
newest_commenters=ArrayFormat(history_in.values('termdelay')),
)
но sql запрос history_out имеет вид:
select "call_schedule_history"."weekday",
array(select U0."termdelay"
from "call_schedule_history" U0
where U0."weekday" = "call_schedule_history"."id") as "newest_commenters"
from "call_schedule_history"
Как я могу заменить "call_schedule_history"."id"
на "call_schedule_history"."weekday"
в Django ORM?