skip to Main Content

I am new in Django
I have this function in views.py

def edituser(request, id):
    member = User.objects.get(id=id)
    return JsonResponse(member, safe=False)

i have the error: Object of type User is not JSON serializable

How can i solve to return User values to javascript ajax function ?

2

Answers


  1. I think that the answer is here: https://linuxpip.org/object-of-type-is-not-json-serializable/ since the User object IS and/or contains non-serializables values, let’s call them "this object contains a thing that have not a Javascript equivalent" check if you have an attribute out of the table in the link provided, to solve this maybe you should filter the "User" object using json.JSONEncoder

    Login or Signup to reply.
  2. If you know the attributes that you want to return from the User object, you could construct a dictionary of those specific attributes:

    def edituser(request, id):
        member = User.objects.get(id=id)
        member_dict = {
            "id": member.id,
            "name": member.name,
            "email": member.email,
            # etc...
        }
        return JsonResponse(member_dict, safe=False)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search