Skip to content
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 #664

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Solution #664

wants to merge 9 commits into from

Conversation

dpiuro
Copy link

@dpiuro dpiuro commented Oct 3, 2024

No description provided.

class Meta:
model = Actor
fields = ("id", "first_name", "last_name", "full_name")

def get_full_name(self, obj):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also can wrote property for Actor method

)

def get_tickets_available(self, movie_session):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also can calculate it in view

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inefficient because we will have to query database for tickets.count() on every entry in a list. Better to override queryset and use annotate()

Copy link

@pashawarganov pashawarganov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice)

for ticket_data in tickets_data:
Ticket.objects.create(order=order, **ticket_data)

return order

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On create/update/delete functions you don't need to return anything

cinema/views.py Outdated
@@ -31,28 +46,90 @@ class CinemaHallViewSet(viewsets.ModelViewSet):


class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.all()
queryset = Movie.objects.prefetch_related("genres", "actors").all()

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 call all()

return super().get_serializer_class()

def get_queryset(self):
queryset = self.queryset

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call super method instead of accesing this attribute directly
solve N+1 problem

cinema/views.py Outdated
return super().get_serializer_class()

def get_queryset(self):
queryset = self.queryset.annotate(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

)

def get_tickets_available(self, movie_session):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inefficient because we will have to query database for tickets.count() on every entry in a list. Better to override queryset and use annotate()

movie = MovieListSerializer(read_only=True)
cinema_hall = CinemaHallSerializer(read_only=True)
taken_places = serializers.SerializerMethodField()
tickets_available = serializers.SerializerMethodField()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be shown only on List serializer

class MovieSessionDetailSerializer(serializers.ModelSerializer):
movie = MovieListSerializer(read_only=True)
cinema_hall = CinemaHallSerializer(read_only=True)
taken_places = serializers.SerializerMethodField()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use nested serializer here instead - create TicketSeatsSerializer with row / seat fields and pass it to taken_places. then you will not need a separate method get_taken_places



class OrderCreateSerializer(serializers.ModelSerializer):
tickets = serializers.ListField()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead create a modified version of ticket serializer without movie_session = MovieSessionListSerializer(read_only=True) and pass it here

cinema/views.py Outdated
return super().get_serializer_class()

def get_queryset(self):
queryset = super().get_queryset()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is better to access queryset like so : self.queryset

cinema/views.py Outdated
def get_queryset(self):
queryset = super().get_queryset()

genres_param = self.request.query_params.get("genres")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of this can go under if condition

cinema/views.py Outdated
queryset = MovieSession.objects.all()
queryset = (
MovieSession.objects.select_related
("movie", "cinema_hall").prefetch_related("tickets").all()

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 prefetch tickets on every action and you don't need all() in the end

cinema/views.py Outdated
return queryset.distinct()


class OrderPagination(PageNumberPagination):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better to put pagination in separate file

cinema/views.py Outdated
Order.objects.prefetch_related
("tickets__movie_session__movie",
"tickets__movie_session__cinema_hall"
).all()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for all() if you already used prefetch

@dpiuro dpiuro requested a review from Oleksl888 October 11, 2024 11:49
cinema/views.py Outdated
Comment on lines 100 to 103
queryset = super().get_queryset().annotate(
available_tickets=F("cinema_hall__rows")
* F("cinema_hall__seats_in_row") - Count("tickets")
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to call it twice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants