Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF Object of type 'User' is not JSON serializable

I'm trying to return the details of the current logged in user in the following way:

from .serializers import UserSerializer

class UserDetailsView(RetrieveAPIView):                                                                                                                                                                           
    model = User                                                                                                                                                                                                  
    queryset = User.objects.all()                                                                                                                                                                                 
    permission_classes = [permissions.IsAuthenticated]                                                                                                                                                            
    serializer_class = UserSerializer                                                                                                                                                                             

    def get(self, request, *args, **kwargs):                                                                                                                                                                      
        user = User.objects.get(id=request.user.id)                                                                                                                                                               
        print(user)                                                                                                                                                                                               
        return Response(user)

in serializers.py I've use ModelSerializer

class UserSerializer(serializers.ModelSerializer):                                                                                                                                                                
      password = serializers.CharField(write_only=True)                                                                                                                                                             

      def create(self, validated_data):                                                                                                                                                                             
          user = UserModel.objects.create(username=validated_data['username'])                                                                                                                                      
          user.set_password(validated_data['password'])                                                                                                                                                             
          user.save()                                                                                                                                                                                               
          return user                                                                                                                                                                                               

      class Meta:                                                                                                                                                                                                   
          model = UserModel                                                                                                                                                                                         
          fields = ('id', 'username', 'password')                                                                                                                                                                   
          write_only_fields = ('password',)

I get an error Object of type 'User' is not JSON serializable. What is wrong in my use case?

like image 203
Ishan Khare Avatar asked Mar 31 '18 18:03

Ishan Khare


3 Answers

You need to return

return Response(serializer.data)

in your get function under UserDetailsView. More details here.

like image 191
Ultrablendz Avatar answered Nov 01 '22 00:11

Ultrablendz


DRF response should be JSON-like objects, to convert model instanse to it you use seralizer class:

def get(self, request, *args, **kwargs):                                                                                                                                                                      
    user = User.objects.get(id=request.user.id)    
    serializer = self.get_serializer(user)                                                                                                                                                                                                                                                                                                                                                    
    return Response(serializer.data)

Also you dont need additional query to get current user, you can user request.user directly:

serializer = self.get_serializer(request.user)    
return Response(serializer.data)
like image 27
neverwalkaloner Avatar answered Oct 31 '22 23:10

neverwalkaloner


Please check you are returning value of particular field in serializer get method, not entire user object.

like image 1
Ankush Sahu Avatar answered Nov 01 '22 01:11

Ankush Sahu