Ad

"Expected A List Of Items But Got Type \"dict\"."

I am trying to build a api for updating first and last name for my user. I am getting the following error in HTTP Response

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

I have written the following API and trying to pass the patch request to it.

class UserSelfUpdateView(UpdateAPIView):
    serializer_class = UserUpdateSerializer
    permission_classes = [UserPermissions, ]

    def update(self, request: Request, *args, **kwargs):
        instance = User.objects.filter(id=self.request.user.id)
        serializer = UserUpdateSerializer(instance, data=request.data, many=True)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response({'success': True}, status=status.HTTP_200_OK)

The serializer for the above request is:

class UserUpdateSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields: ('id', 'first_name', 'last_name')

The format in which I am trying to pass my request body is:

{
    "first_name": "A",
    "last_name": "B"
}

The reason for using

instance = User.objects.filter(id=self.request.user.id)

is because I want the functionality in a way that only logged in user is able to modify his details only.

Ad

Answer

The error is here

serializer = UserUpdateSerializer(instance, data=request.data, many=True)

change to

serializer = UserUpdateSerializer(instance, data=request.data)

if passing many=True you need to pass a queryset not an instance

Ad
source: stackoverflow.com
Ad