Ejemplo n.º 1
0
 def put(self, request, id, format=None):
     product = self.get_object(id)
     serializer = 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)
Ejemplo n.º 2
0
    def post(self,request):
        product_serializer = ProductSerializer(data=request.data)

        file_list=[]
        for count,file_obj in enumerate(request.FILES.getlist("img_path")):
            if file_obj.name.split(".")[1] in ['jpeg','png','jpg','pdf']:
                file_list.append(file_obj)
                # filename = fs.save(MEDIA_ROOT+file_obj.name,file_obj)
            else:
                return Response({"error":"Extension not supported"},status=status.HTTP_400_BAD_REQUEST)

        if product_serializer.is_valid():
            product = product_serializer.save()
        else:
            return Response(product_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
            
        
        for fileobj in file_list:
            fs = FileSystemStorage()
            filename = fs.save(MEDIA_ROOT+fileobj.name,fileobj)
            Images.objects.create(img_path=URL+fileobj.name,img_pid=product.p_id)
            
        image_objects = Images.objects.filter(img_pid = product.p_id).values()
        #change the serializer response to show proper data
        new_dict = {'results': list(image_objects)}
        new_dict.update(product_serializer.data)
        return JsonResponse(new_dict)
Ejemplo n.º 3
0
 def get(self, request, id):
     product = self.get_object(id)
     product = ProductSerializer(product)
     return Response(product.data)
Ejemplo n.º 4
0
 def get(self, request, format=None):
     all_product = Product.objects.all()
     serializer = ProductSerializer(all_product, many=True)
     return Response(serializer.data)