diff --git a/src/back/tests/views.py b/src/back/tests/views.py index 1e8414dd3..1906a5351 100644 --- a/src/back/tests/views.py +++ b/src/back/tests/views.py @@ -55,8 +55,12 @@ class GetTests(generics.ListAPIView): return status_filters.get(status, Test.objects.none()).order_by('dateend') def set_attemps_number(self, user, tests): + test_ids = [test.id for test in tests] + attempt_counts = TestAttempt.objects.filter(user=user, test__id__in=test_ids).values('test').annotate(count=Count('test')) + attempt_counts = {item['test']: item['count'] for item in attempt_counts} + for test in tests: - test.attemps_number = get_attempt_count(user=user, test=test) + test.attemps_number = test.attemps_number - attempt_counts.get(test.id, 0) class TestDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Test.objects.all()