Skip to content

Commit ea06f40

Browse files
committed
Get rid of n+1 queries
1 parent 8af2a7f commit ea06f40

File tree

3 files changed

+12
-43
lines changed

3 files changed

+12
-43
lines changed

learning_resources/models.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,9 @@ def for_serialization(self, *, user: Optional["User"] = None):
386386
),
387387
Prefetch(
388388
"children",
389-
queryset=LearningResourceRelationship.objects.filter(
390-
relation_type=LearningResourceRelationTypes.PROGRAM_COURSES.value,
391-
),
392-
to_attr="_courses",
389+
queryset=LearningResourceRelationship.objects.select_related(
390+
"child"
391+
).order_by("position"),
393392
),
394393
Prefetch(
395394
"user_lists",
@@ -552,17 +551,6 @@ def podcasts(self) -> list["LearningResourceRelationship"]:
552551
),
553552
)
554553

555-
@cached_property
556-
def courses(self) -> list["LearningResourceRelationship"]:
557-
"""Return a list of courses that a resource contains"""
558-
return getattr(
559-
self,
560-
"_courses",
561-
self.children.filter(
562-
relation_type=LearningResourceRelationTypes.PROGRAM_COURSES.value,
563-
).order_by("position"),
564-
)
565-
566554
class Meta:
567555
unique_together = (("platform", "readable_id", "resource_type"),)
568556

learning_resources/serializers.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -444,26 +444,6 @@ class Meta:
444444
fields = ("id", "parent", "child")
445445

446446

447-
class MicroProgramRelationshipSerializer(serializers.ModelSerializer):
448-
"""
449-
Serializer containing only parent and child ids for a program_course_relationship
450-
"""
451-
452-
child = serializers.ReadOnlyField(source="child_id")
453-
readable_id = serializers.ReadOnlyField(source="child.readable_id")
454-
title = serializers.ReadOnlyField(source="child.title")
455-
456-
class Meta:
457-
model = models.LearningResourceRelationship
458-
fields = (
459-
"child",
460-
"position",
461-
"relation_type",
462-
"title",
463-
"readable_id",
464-
)
465-
466-
467447
class LearningResourceMetadataDisplaySerializer(serializers.Serializer):
468448
"""
469449
Serializer to render course information as a text document
@@ -930,13 +910,13 @@ class LearningResourceBaseSerializer(serializers.ModelSerializer, WriteableTopic
930910
resource_category = serializers.SerializerMethodField()
931911
format = serializers.ListField(child=FormatSerializer(), read_only=True)
932912
pace = serializers.ListField(child=PaceSerializer(), read_only=True)
933-
children = MicroProgramRelationshipSerializer(many=True, read_only=True)
913+
children = serializers.SerializerMethodField(allow_null=True)
934914

935-
# @extend_schema_field(LearningResourceRelationshipChildField(allow_null=True))
936-
# def get_children(self, instance):
937-
# return LearningResourceRelationshipChildField(
938-
# instance.courses, many=True, read_only=True
939-
# ).data
915+
@extend_schema_field(LearningResourceRelationshipChildField(allow_null=True))
916+
def get_children(self, instance):
917+
return LearningResourceRelationshipChildField(
918+
instance.children, many=True, read_only=True
919+
).data
940920

941921
def get_resource_category(self, instance) -> str:
942922
"""Return the resource category of the resource"""

learning_resources/views_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ def test_program_endpoint(client, url, params):
174174
def test_program_detail_endpoint(client, django_assert_num_queries, url):
175175
"""Test program endpoint"""
176176
program = ProgramFactory.create()
177-
with django_assert_num_queries(18 + program.learning_resource.children.count()):
177+
assert program.learning_resource.children.count() > 0
178+
with django_assert_num_queries(18): # should be same # regardless of child count
178179
resp = client.get(reverse(url, args=[program.learning_resource.id]))
179180
assert resp.data.get("title") == program.learning_resource.title
180181
assert resp.data.get("resource_type") == LearningResourceType.program.name
@@ -219,7 +220,7 @@ def test_no_excess_queries(rf, user, mocker, django_assert_num_queries, course_c
219220
request = rf.get("/")
220221
request.user = user
221222

222-
with django_assert_num_queries(19):
223+
with django_assert_num_queries(20):
223224
view = CourseViewSet(request=request)
224225
results = view.get_queryset().all()
225226
assert len(results) == course_count

0 commit comments

Comments
 (0)