diff --git a/cinema/serializers.py b/cinema/serializers.py index 280577f26..ee4618983 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -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