1

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, yes I just needed to add partial=True as a param when I call the profile serializer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.