Имеется вот такой запрос
Employee.objects.get(pk=pk).aggregate(
tasks_completed=Count(
'crew_set__crewtask', filter=Q(
crew_set__crew_task_set__status=CrewTaskStatusChoices.SUCCESS
)
),
shifts_completed=Count(
'crew_set__shift', filter=Q(
crew_set__shift__state=ShiftStateChoicesEnum.CLOSE
)
),
proceeds=Sum('crew_set__crewtask__completed_task__received_sum'),
taken_tara=Sum(
'crew_set__crewtask__completed_task__returned_packing__amount'
)
)
Когда я прогоняю его через pytest, мне возвращается ошибка
AttributeError: 'Employee' object has no attribute 'aggregate'
Вот весь трейсбек:
Traceback (most recent call last):
File "/home/anton/.local/share/virtualenvs/expedition-rest-JKEEAQb-/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/anton/.local/share/virtualenvs/expedition-rest-JKEEAQb-/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/anton/.local/share/virtualenvs/expedition-rest-JKEEAQb-/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/anton/.local/share/virtualenvs/expedition-rest-JKEEAQb-/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/anton/.local/share/virtualenvs/expedition-rest-JKEEAQb-/lib/python3.7/site-packages/rest_framework/viewsets.py", line 116, in view
return self.dispatch(request, *args, **kwargs)
File "/home/anton/.local/share/virtualenvs/expedition-rest-JKEEAQb-/lib/python3.7/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/anton/.local/share/virtualenvs/expedition-rest-JKEEAQb-/lib/python3.7/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/anton/.local/share/virtualenvs/expedition-rest-JKEEAQb-/lib/python3.7/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/anton/Рабочий стол/ais/expedition-rest/src/exrest/apps/employees/views.py", line 90, in stats
Employee.objects.get(pk=pk).aggregate(
AttributeError: 'Employee' object has no attribute 'aggregate'
В чём может быть ошибка? Почему он говорит что нет такого атрибута? Соседние эндпоинты с aggregate такую ошибку не вызывают, всё работает
aggregate
используется в QuerySet'ах, а ваш запрос получает только один объект. Отсюда и ошибка