How to serialize db.Model objects to json?

后端 未结 5 527
-上瘾入骨i
-上瘾入骨i 2020-12-08 07:54

When using

from django.utils import simplejson

on objects of types that derive from db.Model it throws exceptions. How to circ

5条回答
  •  有刺的猬
    2020-12-08 08:48

    json cannot be used to serialize anything more than basic types such as dicts, lists, ints/longs, and strings (this is not comprehensive). For example, even these simple commands do not work:

    import json
    json.dumps(object())
    

    If you want to serialize django objects, you should refer to the django documentation on serialization, which will use their own libraries, but they do do support json.

提交回复
热议问题