简体   繁体   中英

Django TypeError: Object of type User is not JSON serializable

So I was trying to make an ajax call in django but this happend

Internal Server Error: /ajax/new_notification/
Traceback (most recent call last):
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\berna\Desktop\Python & Javascript\Web development\MiFamiliaEsUnDesastre\mifamiliaesundesastre\chat\views.py", line 71, in new_notification
    return JsonResponse(user, safe=False)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\http\response.py", line 561, in __init__
    data = json.dumps(data, cls=encoder, **json_dumps_params)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 234, in dumps
    return cls(
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
File "C:\Users\berna\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\serializers\json.py", line 104, in default
    return super().default(o)
File "C:\Users\berna\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
HTTP GET /ajax/new_notification/ 500 [0.15, 127.0.0.1:64660]

the code that is raising this error is here, also I am new in django so maybe the solution can be ver obvious

def new_notification(request):
    user = request.user
    user.profile.notifications = user.profile.notifications + 1
    user.save()

    return JsonResponse(user, safe=False)

thank you for the help

Well, the error is rather self-explanatory:

TypeError: Object of type User is not JSON serializable

Unfortunately, user from within the request object (not necessarily 'user' model, correct me if I'm wrong here) is not JSON-serializable by default. Simplest solution would be to map only necessary fields:

return JsonResponse({ "notifications": profile.notifications }, safe=True)

But that's rather naive. Instead, maybe django serializers could do the job:

from django.core import serializers

def new_notification(request):
    user = request.user
    user.profile.notifications = user.profile.notifications + 1
    user.save()
    
    return JsonResponse(serializers.serialize('json', user), safe=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM