diff --git a/cinema/serializers.py b/cinema/serializers.py index 6de0e6902..cce156eb9 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -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