def post(self, request):
     serializers = ProductsSerializer(data=request.data)
     if serializers.is_valid():
         serializers.save()
         return Response(serializers.data, status=status.HTTP_201_CREATED)
     return Response({'errors': serializers.errors},
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #2
0
 def put(self, request, id):
     measure = get_object(id=id)
     serializer = ProductsSerializer(measure, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def category_products(request, pk):
    if request.method == 'GET':
        category = Category.objects.get(id=pk)
        products = category.products_set.all()
        serializer = ProductsSerializer(products, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = ProductsSerializer(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 put(self, request, pk):
     product = self.get_products(pk)
     serializer = ProductsSerializer(instance=product, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
예제 #5
0
 def post(self, request):
     serializer = ProductsSerializer(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)