Skip to content

Commit

Permalink
Add type annotations to get_queryset method
Browse files Browse the repository at this point in the history
  • Loading branch information
Viacheslav417865 committed Dec 2, 2024
1 parent eab8adf commit b7e7d4e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions cinema/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Type
from django.db.models import QuerySet
import serializers
from rest_framework import viewsets
from cinema.models import (
Movie,
Expand All @@ -14,21 +17,22 @@
MovieSessionSerializer,
MovieSessionListSerializer,
MovieRetrieveSerializer,
MovieSessionRetrieveSerializer, MovieListSerializer,
MovieSessionRetrieveSerializer,
MovieListSerializer,
)


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.all()

def get_serializer_class(self):
def get_serializer_class(self) -> Type[serializers.Serializer]:
if self.action == "list":
return MovieSessionListSerializer
elif self.action == "retrieve":
return MovieSessionRetrieveSerializer
return MovieSessionSerializer

def get_queryset(self):
def get_queryset(self) -> QuerySet:
queryset = self.queryset
if self.action in ("list", "retrieve"):
return queryset.select_related()
Expand All @@ -53,14 +57,14 @@ class CinemaHallViewSet(viewsets.ModelViewSet):
class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.all()

def get_serializer_class(self):
def get_serializer_class(self) -> Type[serializers.Serializer]:
if self.action == "list":
return MovieListSerializer
elif self.action == "retrieve":
return MovieRetrieveSerializer
return MovieSerializer

def get_queryset(self):
def get_queryset(self) -> QuerySet:
queryset = self.queryset
if self.action in ("list", "retrieve"):
return queryset.prefetch_related("genres", "actors")
Expand Down

0 comments on commit b7e7d4e

Please sign in to comment.