def post(self, request): data = JSONParser().parse(request) serializer = IngredientSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=status.HTTP_201_CREATED) return JSONResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, pk): try: data = Ingredient.objects.get(pk=pk) except Ingredient.DoesNotExist: return HttpResponse(status=status.HTTP_404_NOT_FOUND) parsed_data = JSONParser().parse(request) serializer = IngredientSerializer(data, data=parsed_data, partial=True) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)