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 #658

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

Solution #658

wants to merge 4 commits into from

Conversation

raychw
Copy link

@raychw raychw commented Sep 15, 2024

No description provided.

Copy link

@Dimosphen1 Dimosphen1 left a comment

Choose a reason for hiding this comment

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

Make sure that all checks pass. Several changes were requested

Choose a reason for hiding this comment

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

Do not redefine an existing migration, create a new one instead

cinema/views.py Outdated
if request.method == "POST":
serializer = MovieSerializer(data=request.data)
def post(self, request):
serializer = GenreSerializer(data=request.data)
if serializer.is_valid():

Choose a reason for hiding this comment

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

Use raise_exception=True here and in the other places as well to remove the if on condition while validating serializer

@raychw raychw requested a review from Dimosphen1 September 15, 2024 13:56
cinema/views.py Outdated
Comment on lines 94 to 96
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
Copy link

Choose a reason for hiding this comment

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

  1. Avoid using an if condition to check if a serializer is valid. Instead, use the raise_exception=True flag when calling serializer.is_valid(). This will automatically raise a ValidationError if the data is invalid, which is then caught by the DRF exception handler to return a 400 Bad Request response.

Good example:

if request.method == 'POST':
    serializer = MovieSerializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    serializer.save()
    return Response(serializer.data, status=status.HTTP_201_CREATED)

Bad example:

if request.method == 'POST':
    serializer = MovieSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@raychw raychw requested a review from vsmutok September 15, 2024 18:37
Copy link

@viktoria-rybenchuk viktoria-rybenchuk left a comment

Choose a reason for hiding this comment

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

LGTM!

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.

4 participants