Skip to content

Commit

Permalink
delete same code
Browse files Browse the repository at this point in the history
  • Loading branch information
Familenko committed Nov 30, 2023
1 parent d3c9fba commit 3399024
Showing 1 changed file with 1 addition and 31 deletions.
32 changes: 1 addition & 31 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,17 @@ class MovieSerializer(serializers.Serializer):
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):
actors = validated_data.pop("actors")
genres = validated_data.pop("genres")

movie = Movie.objects.create(**validated_data)
movie.actors.set(actors)
movie.genres.set(genres)

return movie
return Movie.objects.create(**validated_data)

def update(self, instance, validated_data):
actors_data = validated_data.pop("actors", [])
genres_data = validated_data.pop("genres", [])

instance.title = validated_data.get("title", instance.title)
instance.description = validated_data.get(
"description", instance.description
)
instance.duration = validated_data.get("duration", instance.duration)

is_partial = self.context["request"].method == "PATCH"

if actors_data:
if is_partial:
instance.actors.add(*actors_data)
else:
instance.actors.set(actors_data)

if genres_data:
if is_partial:
instance.genres.add(*genres_data)
else:
instance.genres.set(genres_data)

instance.save()

return instance
Expand Down

0 comments on commit 3399024

Please sign in to comment.