diff --git a/cinema/models.py b/cinema/models.py index d48cc7ca1..7cbc8cf61 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -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 diff --git a/cinema/views.py b/cinema/views.py index 4795422a1..360279a29 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -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): @@ -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) diff --git a/requirements.txt b/requirements.txt index 56e135547..99938e15f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +djangorestframework==3.13.1