django 将queryset中datetime序列化json
import json import datetime class DjangoJSONEncoder(json.JSONEncoder): def default(self, o): # See "Date Time String Format" in the ECMA-262 specification. if isinstance(o, datetime.datetime): r = o.strftime('%Y-%m-%d %H:%M:%S') return r elif isinstance(o, datetime.date): return o.isoformat() elif isinstance(o, datetime.time): r = o.isoformat() if o.microsecond: r = r[:12] return r else: return super().default(o)
示例
temp_output=MyModel.objects.all() output = json.dumps(temp_output, cls=DjangoJSONEncoder)
还没有留言,还不快点抢沙发?