Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
TsvetanKichuk committed Sep 28, 2024
1 parent 6b352db commit 5e46010
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cinema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Movie(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
duration = models.IntegerField()
genres = models.ManyToManyField(Genre)
actors = models.ManyToManyField(Actor)
genres = models.ManyToManyField(Genre, related_name="movies")
actors = models.ManyToManyField(Actor, related_name="movies")

def __str__(self):
return self.title, self.description, self.duration, self.genres
Expand Down
21 changes: 9 additions & 12 deletions cinema/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ def get(self, request):

def post(self, request):
serializer = GenreSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)


class GenreDetail(APIView):
Expand All @@ -38,18 +37,16 @@ def get(self, request, pk):
def put(self, request, pk):
genre = self.get_object(pk)
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)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)

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)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)

def delete(self, request, pk):
genre = self.get_object(pk)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ flake8-quotes==3.3.1
flake8-variables-names==0.0.5
pep8-naming==0.13.2
django-debug-toolbar==3.2.4
djangorestframework==3.13.1
djangorestframework==3.13.1

0 comments on commit 5e46010

Please sign in to comment.