Django rest framework error

85 views
Skip to first unread message

Soumen Khatua

unread,
Feb 20, 2020, 12:40:41 PM2/20/20
to django...@googlegroups.com
Hi Folks,

I'm getting this error, I don't know how to solve it:

File "C:\Users\TildeHat\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type User is not JSON serializable


This is my code:

Views.py:
--------------

class LocationApiView(APIView):
    permission_classes = (IsAuthenticated,)
    def get(self,request,format=None):
        user = request.user
        user_location = Profile.objects.get(user = user)
        return Response({'user':user}, status = status.HTTP_200_OK)


urls.py
----------------

urlpatterns = [
    path('api/location/',views.LocationApiView.as_view(),name = 'api_location')

]


btw when I'm sending a normal message it's working fine. But in this case I'm getting this error.

Thank You

Regards,
Soumen

MTS BOUR

unread,
Feb 20, 2020, 12:49:02 PM2/20/20
to django...@googlegroups.com
Can you show us your serializer.py file?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WZh%2BgVfi7URo8vPbpjYfT%3D5AdjGyhntBMLgiZ1msiZECg%40mail.gmail.com.

Soumen Khatua

unread,
Feb 20, 2020, 1:00:18 PM2/20/20
to django...@googlegroups.com
If I'm using serializer then it's working fine but I want to return only database object. So for that Do I need to add serializer.

Thank you for your response. 

onlinejudge95

unread,
Feb 20, 2020, 1:10:39 PM2/20/20
to django...@googlegroups.com
On Thu, Feb 20, 2020 at 11:30 PM Soumen Khatua <soumenk...@gmail.com> wrote:
If I'm using serializer then it's working fine but I want to return only database object. So for that Do I need to add serializer.
You do have to serialize your responses, you just can't send Python objects as an HTTP response, think about the content-type of your requests

MTS BOUR

unread,
Feb 20, 2020, 1:59:47 PM2/20/20
to django...@googlegroups.com
Try
 from django.http import JsonResponse
And use jsonresponse instead


Reply all
Reply to author
Forward
0 new messages