2

I noticed that I with Django Rest Framework 3 no longer have to specify files=request.FILES when using a serializer in file uploads. According to the release notes, request.data now contains all the data. So, I simply removed files=request.FILES, and went from:

serializer = MediaSerializer(
    theMedia,
    data = postData,
    files = request.FILES,
    partial = True
)

to:

serializer = MediaSerializer(
    theMedia,
    data = postData,
    partial = True
)

In my Django API app, I have a /media/ endpoint to which one can PUT the files photo and filename. When trying to PUT files with the change I made, the files never touch my custom storage. The header is correct (Content-Type: multipart/form-data), and it all worked fine in DRF 2.x.

I Noticed that I need to use the MultiPartParser in my view to be able to use the fileField, so now my view uses parser_classes = (JSONParser, MultiPartParser).

Here is the section of the serializer handeling the files:

class MediaSerializer(serializers.ModelSerializer):
    photo = serializers.ImageField(
        max_length = None,
        required = False,
        allow_empty_file = False)
    filename = serializers.FileField(
        max_length = None,
        required = False,
        allow_empty_file = False)
    ...

class Meta:
    model = Media
    fields = (
        'photo',
        'filename'
    )

Maybe I should also mention that I have turned off UPLOADED_FILES_USE_URL in the settings.

My question is: What is it that I am missing out in DRF3 - Why does the uploaded files never touch my custom storage?

0

1 Answer 1

3

Django REST Framework 3 deprecated the request.DATA and request.FILES properties in favor of a unified request.data property. The serializers were also updated as well to remove the files argument, still maintaining the data argument which should be request.data instead of request.DATA.

In your example, you appear to be altering the incoming data, as you have it set to a variable postData, which isn't included. Most likely, you are not using request.data and the issue is that you are only getting the data without the files, instead of the full data you are expecting.

You should be able to verify this by logging the values of request.data and postData, and check that request.data contains the image you are trying to send.

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

1 Comment

Kevin, you are very right. I alter the data: postData = request.data.copy(). Copying this way does not include the files. Thank you for pointing me towards the right direction - I appriciate it alot!

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.