[Django]-Non_field_errors : ["Expected a list of items but got type "dict"."]

10👍

The trouble is not with the slug. It is that you have used many = True in the view when you pass the data to the serializer, but you are in fact only sending a single message – which is why it is a dict and not a list. Remove that parameter.

def post(self, request, formate = None):
    serializer = MesSerializer(data=request.data)

3👍

I recently came across a problem which is similar to the OP. Hence would like to share my experience and solution.

I have a dictionary of items, each of them unique. I wanted to use PUT to update an existing item. So I used objects.filter to fetch the object based on the name passed via JSON Request(I know I should have used pk, but I didn’t because the items are unique and will remain so). Then I created a Django REST Serializer class object to save the updated object but I failed. This is because I wasn’t using many = True. But when I did use it, I faced another error:

"non_field_errors": [
        "Expected a list of items but got type \"dict\"."
    ]

So I finally removed both many=True as well objects.filter.
Instead I used objects.get. This solved the problem because objects.get returns the required object which I want to update whereas objects.filter returns a queryset object and not the actual object that I want to update. Of course objects.get will fail if I have multiple results, in which case I need to ensure there is a pk. Then again in my case objects.get will never return more than one object.

Hope this post helps someone and saves a lot of their time. 🙂

Leave a comment