From cbe7c21f75974775d15d645fb1a169d49cf00182 Mon Sep 17 00:00:00 2001 From: user Date: Wed, 11 Dec 2024 18:45:31 +0200 Subject: [PATCH] impfix N+1 problem --- cinema/serializers.py | 1 + cinema/views.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cinema/serializers.py b/cinema/serializers.py index 20deb7fc..9d474249 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -129,6 +129,7 @@ class Meta: "cinema_hall", "movie", ) + read_only_fields = ("id",) class MovieSessionDetailSerializer(serializers.ModelSerializer): diff --git a/cinema/views.py b/cinema/views.py index 6f136ff5..d43aafa7 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -34,7 +34,7 @@ class CinemaHallViewSet(viewsets.ModelViewSet): class MovieViewSet(viewsets.ModelViewSet): - queryset = Movie.objects.all() + queryset = Movie.objects.prefetch_related() def get_serializer_class(self): if self.action == "list": @@ -45,7 +45,7 @@ def get_serializer_class(self): class MovieSessionViewSet(viewsets.ModelViewSet): - queryset = MovieSession.objects.all() + queryset = MovieSession.objects.select_related("movie", "cinema_hall") def get_serializer_class(self): if self.action == "retrieve":