Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
 def post(self, request):
     serializer = DishSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors)