def put(self, request, pk, format=None): product = self.get_object(pk) serializer = serializers.ProductSerializer(product, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): serializer = serializers.ProductSerializer(data=request.data) if serializer.is_valid(): serializer.save() producer.publish_product(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'msg': 'bad request'}, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, *args, **kwargs): product = self.get_object(pk=kwargs['pk']) serializer = serializers.ProductSerializer( product, data=request.data, partial=True) # set partial=True to update a data partially if serializer.is_valid(): serializer.save() return Response(serializer.data, status=204) return Response(data="wrong parameters", code=400)
def get(self, request, pk, format=None): product = self.get_object(pk) data = Product.objects.filter(id=product.id).values()[0] serializer = serializers.ProductSerializer(data=data) if serializer.is_valid(raise_exception=True): return Response(serializer.data, status=200) return Response(serializer.errors, status=400)
def post(self, request, format=None): product = request.data serializer = serializers.ProductSerializer(data=product) if serializer.is_valid(raise_exception=True): serializer.save() # print(serializer.data) return Response(serializer.data, status=201) return Response(serializer.errors, status=400)
def update(self, request, pk=None): product = self.find_product(pk) if not product: return Response({'msg': 'product not found'}, status=status.HTTP_404_NOT_FOUND) serializer = serializers.ProductSerializer( instance=product, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_202_ACCEPTED) return Response({'msg': 'bad request', 'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): product = list(Product.objects.values()) serializer = serializers.ProductSerializer(data=product, many=True) if serializer.is_valid(raise_exception=True): return Response(serializer.data, status=200) return Response(serializer.errors, status=400)
def get(self, request, pk, *args, **kwargs): products = product_models.Product.objects.filter(store_id=pk).all() return Response(data=product_serializers.ProductSerializer( products, many=True).data)
def retrieve(self, request, pk=None): product = self.find_product(pk) if not product: return Response({'msg': 'product not found'}, status=status.HTTP_404_NOT_FOUND) serializer = serializers.ProductSerializer(instance=product) return Response(serializer.data, status=status.HTTP_200_OK)
def list(self, request): products = models.Product.objects.all() serializer = serializers.ProductSerializer(products, many=True) return Response(serializer.data, status=status.HTTP_200_OK)