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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cinema/serializes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from rest_framework import serializers

from cinema.models import Movie


class MovieSerializer(serializers.Serializer):

Choose a reason for hiding this comment

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

Consider using serializers.ModelSerializer instead of serializers.Serializer. This will automatically generate the create and update methods based on the model fields, reducing boilerplate code.

id = serializers.IntegerField(read_only=True)
title = serializers.CharField(required=True)
description = serializers.CharField(required=True)
duration = serializers.IntegerField(required=True)
def create(self, validated_data):
return Movie.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.title = validated_data.get("title", instance.title)
instance.description = validated_data.get(
"description",
instance.description
)
instance.duration = validated_data.get(
"duration",
instance.duration
)
instance.save()
return instance
8 changes: 8 additions & 0 deletions cinema/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from cinema.views import movie_list, movie_detail

urlpatterns = [
path("movies/", movie_list, name="movie-list"),
path("movies/<int:pk>/", movie_detail, name="movie-detail"),
]
app_name = "cinema"