[Django]-"non_field_errors": [ "Invalid data. Expected a dictionary, but got list." ] in django rest framework while calling api in postman

10👍

Based on your Postman screenshot, you’re sending a list (denoted by []).

"billing_details": [
    {
     "user":34,
     "first_name":"Mylo",
     "last_name":"Don"
    }
]

If you remove the brackets so your POST request contains a dictionary (denoted by {}) then you shouldn’t see this error anymore.

"billing_details": {
     "user":34,
     "first_name":"Mylo",
     "last_name":"Don"
}

If you would like to be able to send multiple requests to billing_details at a time then you can update your serializers.py like so:

class OrderSerializer(serializers.ModelSerializer):
    billing_details = BillingDetailsSerializer(many=True)

This will also make your original POST request work since passing the many=True parameter causes the serializer to expect a list, in this case it will be a list of length 1.

-2👍

While hitting the api please send body data enclosed in a "[]".

Leave a comment