TypeError: Python object is not JSON serializable

前端 未结 3 957
慢半拍i
慢半拍i 2021-01-04 23:58

I am trying to encode an object into json using json.dumps() in Django, however when I pass in a python object, it raises this error.

TypeError         


        
3条回答
  •  粉色の甜心
    2021-01-05 00:37

    object is not one of those types. Dictionaries, lists (maybe tuples), floats, strings, integers, bools, and None I believe are the types that Python can serialize into JSON natively.

    However, it looks like Django has some built-in serializers that may work for you.

    I'm guessing that

    from django.core import serializers
    data = serializers.serialize("json", OrgInvite.objects.filter(token=100))
    

    should work for you

提交回复
热议问题