Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
sberdianskyi committed Nov 3, 2024
1 parent 4220cec commit 0e17cf4
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,30 @@ def update(self, instance, validated_data):
return instance


class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = "__all__"
class MovieSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
title = serializers.CharField(max_length=255)
description = serializers.CharField()
duration = serializers.IntegerField()
actors = serializers.PrimaryKeyRelatedField(
queryset=Actor.objects.all(),
many=True
)
genres = serializers.PrimaryKeyRelatedField(
queryset=Genre.objects.all(),
many=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

0 comments on commit 0e17cf4

Please sign in to comment.