2

I'm trying to upload images by using DRF:

I have simple serializer:

class ImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Image
        fields = ('file',)

This file field is simple ImageField in my model.

Then I have created viewset:

class ImagesViewSet(ModelViewSet):
    serializer_class = ImageSerializer
    queryset = Image.objects.all()
    permission_classes = (IsAuthenticated,)

    parser_classes = (MultiPartParser, FormParser)

    @action(methods=['post'], detail=False,
            permission_classes=[IsAuthenticated])
    def upload_avatar(self, request):
        print(request.data)
        return Response({"image": "ok"})

I just want to print what I'm trying to upload.

After makeing request to upload data:

upload2

My Query Dict returns me:

<QueryDict: {}>

Is it bug? Or I did miss something?

Version of DRF: 3.8.2

Django: 2.0.6

5
  • 1
    @neverwalkaloner Already did this, still empty. Commented Jun 23, 2018 at 13:33
  • 1
    It may be postman bug. try to remove Content-Type from request header in postman before sending request. Sometime postman doesn't set correct header(content type).that may be the problem. Commented Jun 23, 2018 at 14:20
  • 1
    It may be postman bug. try to remove Content-Type from request header in postman before sending request. Commented Jun 23, 2018 at 14:22
  • @Kaushal. It worked. Strange. This is very annoying error. Please can you write it as an answer so I could accept it? Commented Jun 23, 2018 at 14:28
  • sure it will help other users also Commented Jun 23, 2018 at 14:29

2 Answers 2

3

It may be postman bug. try to remove Content-Type from request header in postman before sending request.

Sometime postman doesn't set correct header(content type).that may be the problem.

Sign up to request clarification or add additional context in comments.

Comments

1

How about django's request.FILES instead of request.data?? Django Docs File Uploads says

When Django handles a file upload, the file data ends up placed in request.FILES..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.