def put(self, request, pk=None): dish = request.get_object(pk) serializer = DishSerializer(instance=dish, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def put(self, request, id): dish = self.get_object(id) serializer = DishSerializer(instance=dish, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def dish_list(request): if request.method == 'GET': dish = Dish.objects.filter(name__contains='5').order_by('-id') serializer = DishSerializer(dish, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = DishSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def dish_detail(request, about_id): try: about = Dish.objects.get(id=about_id) except Dish.DoesNotExist as e: return Response({'message': str(e)}, status=400) if request.method == 'GET': serializer = DishSerializer(about) return Response(serializer.data) elif request.method == 'PUT': serializer = DishSerializer(instance=about, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors) elif request.method == 'DELETE': about.delete() return Response({'message': 'deleted'}, status=204)
def post(self, request): serializer = DishSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)