Django REST - Invalid data. Expected a dictionary, but got {datatype}

Viewed 913

Using DRF I'm attempting a PUT request to update a file in my database.

I'm receiving the error seen in the title, exactly Invalid data. Expected a dictionary, but got {datatype}..

The full body says this (serializer.error_messages):

{
    "required": "This field is required.",
    "null": "This field may not be null.",
    "invalid": "Invalid data. Expected a dictionary, but got {datatype}."
}

I don't see any additional helpful information in the terminal, just this exactly.

My model is this:

from django.db import models

class File(models.Model):
    name = models.CharField(max_length=1024)
    upload_timestamp_date = models.DateField(auto_now_add=True)
    upload_timestamp_time = models.TimeField(auto_now_add=True)
    file = models.FileField()

    def __str__(self):
        return self.name

My serializer is this:

from rest_framework import serializers
from .models import File

class FileSerializer(serializers.ModelSerializer):
    class Meta:
        model = File
        fields = '__all__'

My view so far is this:

@api_view(['GET', 'PUT', 'DELETE'])
@csrf_exempt
def file(request, file_id, format=None):
    try:
        data = File.objects.get(pk=file_id)
    except File.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = FileSerializer(data)
        return Response({'file': serializer.data}, status=status.HTTP_200_OK)

    elif request.method == 'PUT':
        serializer = FileSerializer(data, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.error_messages, status=status.HTTP_400_BAD_REQUEST)

It comes down to the is_valid() call being false, with those error messages.

I know that a GET request returns data like this:

{
    "file": {
        "id": 1,
        "name": "test",
        "upload_timestamp_date": "2022-04-16",
        "upload_timestamp_time": "17:02:56.790144",
        "file": "https://files-learning.s3.amazonaws.com/akash_nemw.jpg"
    }
}

I have been submitting this data in the body (only changing the name):

{
        "id": 1,
        "name": "testing change",
        "upload_timestamp_date": "2022-04-16",
        "upload_timestamp_time": "17:02:56.790144",
        "file": "https://files-learning.s3.amazonaws.com/akash_nemw.jpg"
}

Please, any help would be greatly appreciated, I have spent a day trying to figure out what is wrong with the submitted data.

1 Answers

The solution to this (as suggested in a comment) was to replace error_messages with errors. I am unsure where I got the idea to use error_messages, but it was a small mistake leading to lots of frustration.

This lead to the error The submitted data was not a file. Check the encoding type on the form. I actually needed to define the request in the form-data section of Postman with the file drop down. This allowed me to choose a file from my computer instead of reusing the same S3 URL.

Related