Skip to content

Commit 3c78c63

Browse files
authored
Merge pull request #570 from PROCOLLAB-github/feature/add-scored-expert-id
Добавлен ID эксперта в данные об оценённых проектах
2 parents 386936c + 172b6e1 commit 3c78c63

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

project_rates/serializers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ProjectListForRateSerializer(serializers.ModelSerializer):
6161
views_count = serializers.SerializerMethodField()
6262
criterias = serializers.SerializerMethodField()
6363
scored = serializers.SerializerMethodField()
64+
scored_expert_id = serializers.IntegerField(read_only=True, allow_null=True)
6465

6566
class Meta:
6667
model = Project
@@ -75,6 +76,7 @@ class Meta:
7576
"region",
7677
"views_count",
7778
"scored",
79+
"scored_expert_id",
7880
"criterias",
7981
]
8082

project_rates/views.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.contrib.auth import get_user_model
2-
from django.db.models import QuerySet, Count
2+
from django.db.models import QuerySet, Count, OuterRef, Subquery, IntegerField
33

44
from rest_framework import generics, status
55
from rest_framework.response import Response
@@ -98,6 +98,13 @@ def get_queryset(self) -> QuerySet[Project]:
9898
project__isnull=False, partner_program__id=self.kwargs.get("program_id")
9999
).values_list("project__id", flat=True)
100100
# `Count` the easiest way to check for rate exist (0 -> does not exist).
101+
scored_expert_subquery = ProjectScore.objects.filter(
102+
project=OuterRef("pk")
103+
).values("user__expert__id")[:1]
104+
101105
return Project.objects.filter(draft=False, id__in=projects_ids).annotate(
102-
scored=Count("scores")
106+
scored=Count("scores"),
107+
scored_expert_id=Subquery(
108+
scored_expert_subquery, output_field=IntegerField()
109+
),
103110
)

0 commit comments

Comments
 (0)