You cannot call `.save()` after accessing ``


Error Showing when post data from API

You cannot call `.save()` after accessing ``.If you need to access data before committing to the database then inspect 'serializer.validated_data' instead. 

My written Code is:

serializerdata = serializers.CreateSerializer(
if serializerdata.is_valid():
    user_id ='user_id')
    if user_id==2:
        return JsonResponse({"message": "success"}) 
        return JsonResponse({"message": "user invalid"})    
    return JsonResponse({"message": "error"})   

Asked By: Chandan Sharma



As the error says, you can not access the and then call save().

You should instead user validated_data:

user_id = serializerdata.validated_data.get('user_id')
Answered By: Nico Griffioen

Take care that in your debugger watch there is no listed.
It will read out the data before is invoked and lead to this confusing behaviour. Took me at least an hour to figure it out.

Answered By: A. Lang