Pawan Pawan - 6 months ago 146
AngularJS Question

Django Rest Framework File Upload

I am using Django Rest Framework and AngularJs to upload a file. My view file looks like this.

class ProductList(APIView):
authentication_classes = (authentication.TokenAuthentication,)
def get(self,request):
if request.user.is_authenticated():
userCompanyId = request.user.get_profile().companyId
products = Product.objects.filter(company = userCompanyId)
serializer = ProductSerializer(products,many=True)
return Response(

def post(self,request):
serializer = ProductSerializer(data=request.DATA, files=request.FILES)
if serializer.is_valid():
return Response(data=request.DATA)

As the last line of post method should return all the data , what should I write there , or how to check there if there is anything in request.FILES , and how to serializer filefield or how should I use Parser? please help..


Use the FileUploadParser, it's all in the request. Use a put method instead, you'll find an example in the docs :)

class FileUploadView(views.APIView):
    parser_classes = (FileUploadParser,)

    def put(self, request, filename, format=None):
        file_obj = request.FILES['file']
        # do some stuff with uploaded file
        return Response(status=204)