-
Notifications
You must be signed in to change notification settings - Fork 636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Solution" #656
base: master
Are you sure you want to change the base?
"Solution" #656
Conversation
order = Order.objects.create(**validated_data) | ||
for ticket_data in tickets_data: | ||
Ticket.objects.create(order=order, **ticket_data) | ||
return order |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In create/update/delete function you don't need to return anything
cinema/views.py
Outdated
@@ -11,7 +14,7 @@ | |||
MovieSessionListSerializer, | |||
MovieDetailSerializer, | |||
MovieSessionDetailSerializer, | |||
MovieListSerializer, | |||
MovieListSerializer, OrderSerializer, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each from new line)
cinema/views.py
Outdated
if genres: | ||
genres_ids = [int(str_id) for str_id in genres.split(",")] | ||
queryset = queryset.filter(genres__id__in=genres_ids) | ||
|
||
actors = self.request.query_params.get("actors") | ||
if actors: | ||
actors_ids = [int(str_id) for str_id in actors.split(",")] | ||
queryset = queryset.filter(actors__id__in=actors_ids) | ||
|
||
title = self.request.query_params.get("title") | ||
if title: | ||
queryset = queryset.filter(title__icontains=title) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to check all of these things only when self.action == "list"
cinema/views.py
Outdated
|
||
actors = self.request.query_params.get("actors") | ||
if actors: | ||
actors_ids = [int(str_id) for str_id in actors.split(",")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have a duplicates, create a separate function for this
cinema/views.py
Outdated
@@ -48,6 +73,32 @@ class MovieSessionViewSet(viewsets.ModelViewSet): | |||
queryset = MovieSession.objects.all() | |||
serializer_class = MovieSessionSerializer | |||
|
|||
def get_queryset(self): | |||
queryset = MovieSession.objects.all() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First will be good to solve N+1 problem
So you can wrap your code in if self.actions == "list"
also
And don't forget about retrieve
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should fix n+1 with movie and cinema_hall
cinema/serializers.py
Outdated
|
||
class Meta: | ||
model = Ticket | ||
fields = ("id", "row", "seat", "movie_session", "order") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need to display order on a ticket - in this task we actually display a ticket inside of the Order
cinema/serializers.py
Outdated
class MovieSessionDetailSerializer(MovieSessionSerializer): | ||
movie = MovieListSerializer(many=False, read_only=True) | ||
cinema_hall = CinemaHallSerializer(many=False, read_only=True) | ||
taken_places = TakenPlacesSerializer( | ||
source="tickets", | ||
many=True, read_only=True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
each argument from new line please
cinema/urls.py
Outdated
@@ -6,7 +6,7 @@ | |||
ActorViewSet, | |||
CinemaHallViewSet, | |||
MovieViewSet, | |||
MovieSessionViewSet, | |||
MovieSessionViewSet, OrderViewSet, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please move to the new line
model = Ticket | ||
fields = ("row", "seat") | ||
|
||
|
||
class MovieSessionDetailSerializer(MovieSessionSerializer): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What bout MovieSessionSerializer that will be used to create movie session - it must have a bit different signature
fields = ("id", "show_time", "movie", "cinema_hall", "taken_places") | ||
|
||
|
||
class OrderSerializer(serializers.ModelSerializer): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about OrderSerializer that will be used with list action? It will have a different signature
if self.action == "retrieve": | ||
return MovieDetailSerializer | ||
|
||
return MovieSerializer | ||
|
||
|
||
class MovieSessionViewSet(viewsets.ModelViewSet): | ||
queryset = MovieSession.objects.all() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need this if you define get_queryset method
cinema/views.py
Outdated
@@ -48,6 +73,32 @@ class MovieSessionViewSet(viewsets.ModelViewSet): | |||
queryset = MovieSession.objects.all() | |||
serializer_class = MovieSessionSerializer | |||
|
|||
def get_queryset(self): | |||
queryset = MovieSession.objects.all() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should fix n+1 with movie and cinema_hall
total_capacity=F( | ||
"cinema_hall__rows") * F("cinema_hall__seats_in_row"), | ||
tickets_sold=Count("tickets"), | ||
tickets_available=F("total_capacity") - F("tickets_sold") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tickets available and taken seats must be displayed on different actions and different serializers
|
||
|
||
class OrderViewSet(viewsets.ModelViewSet): | ||
queryset = Order.objects.all() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
N+1 problem is present here since you access a lot of nested fields
|
||
class OrderViewSet(viewsets.ModelViewSet): | ||
queryset = Order.objects.all() | ||
serializer_class = OrderSerializer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you will need to add a different serializer for list action and override get_serializer_class method here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
No description provided.