Ryan Saxe
Ryan Saxe

Reputation: 17869

Django rest framework not reading JSON right

So I am using a basic API view as such:

@api_view(['GET'])
def load_info(request,user_id):
    user = User.objects.get(pk=user_id)
    profile = user.profile
    serialized = ProfileSerializerInfo(profile,data=request.DATA)
    print serialized.data
    if serialized.is_valid():
        return Response(serialized)
    else:
        return Response(serialized.errors)

now the print serialized.data returns the full amount of information with a user, yet the JSON returned is the serialized.errors, which says:

{
    "user": [
        "This field is required."
    ]
}

why is Django rest framework not noticing the user field in the JSON?

Note

this is what serialized.data looks like:

{'user': {u'id': 22, ...}, 'follower_count':3452,...}

I also tried passing serialized.data to Response, but that did not work either.

by request, here is the serializer:

class ProfileSerializerInfo(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = Profile

Upvotes: 1

Views: 304

Answers (1)

xxx
xxx

Reputation: 1485

Could you post the definition of ProfileSerializerInfo and are you using nested serializers?

You could try user = UserSerializer(many=False, required=False) on ProfileSerializerInfo

Also found this from here:

You need to use partial=True to update a row with partial data:

serializer = UserSerializer(user, data=request.DATA, partial=True)

From docs:

By default, serializers must be passed values for all required fields or they will throw validation errors. You can use the partial argument in order to allow partial updates.

Upvotes: 4

Related Questions