Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
markus2812 committed Nov 2, 2023
1 parent e1acb4f commit 1d15be5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 6 additions & 4 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ def update(self, instance, validated_data):
instance.duration
)

actors_data = validated_data.pop("actors")
genres_data = validated_data.pop("genres")
actors_data = validated_data.get("actors")
genres_data = validated_data.get("genres")

instance.actors.set(actors_data)
instance.genres.set(genres_data)
if actors_data is not None:
instance.actors.set(actors_data)
if genres_data is not None:
instance.genres.set(genres_data)

instance.save()

Expand Down
23 changes: 22 additions & 1 deletion cinema/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ActorDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView
):
queryset = Actor.objects.all()
Expand All @@ -60,6 +61,14 @@ def get(self, request, *args, **kwargs):
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)

def patch(self, request, *args, **kwargs):
actor = self.get_object()
serializer = ActorSerializer(actor, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)

Expand Down Expand Up @@ -90,7 +99,19 @@ def get(self, request, pk):

def put(self, request, pk):
genre = self.get_object(pk)
serializer = GenreSerializer(genre)
serializer = GenreSerializer(genre, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def patch(self, request, pk):
genre = self.get_object(pk)
serializer = GenreSerializer(
genre,
data=request.data,
partial=True
)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
Expand Down

0 comments on commit 1d15be5

Please sign in to comment.