There is such a request:

stats = CompletedTask.objects.filter( Q(created_date__gt=serializer.data['begin_date']) & Q(created_date__lt=serializer.data['end_date']) ).values(shift_type_id=F('task__shift__type__id')).annotate( count=Count('task__shift__type__id') ) 

I get the data in this form:

 [ { "shift_type_id": 4, "count": 2 }, { "shift_type_id": 5, "count": 1 } ] 

What are the ways to obtain data in this form:

 {4: 2, 5: 1} 
  • Show the code serializer and view. - avtomato

1 answer 1

And what prevents to convert the data?

 # Данные в полученном формате raw = [{"shift_type_id": 4, "count": 2}, {"shift_type_id": 5, "count": 1}] # Преобразование к простому словарю res = {el["shift_type_id"]: el["count"] for el in raw} # Теперь res = {4: 2, 5: 1}