Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么DjangoJSONEncoder不处理代理对象?_Python_Json_Django - Fatal编程技术网

Python 为什么DjangoJSONEncoder不处理代理对象?

Python 为什么DjangoJSONEncoder不处理代理对象?,python,json,django,Python,Json,Django,这是来自DjangoJSONEncoder的代码,它处理标准json编码器之外的大多数需要: class DjangoJSONEncoder(json.JSONEncoder): """ JSONEncoder subclass that knows how to encode date/time and decimal types. """ def default(self, o): # See "Date Time String Format"

这是来自DjangoJSONEncoder的代码,它处理标准json编码器之外的大多数需要:

class DjangoJSONEncoder(json.JSONEncoder): """ JSONEncoder subclass that knows how to encode date/time and decimal types. """ def default(self, o): # See "Date Time String Format" in the ECMA-262 specification. if isinstance(o, datetime.datetime): r = o.isoformat() if o.microsecond: r = r[:23] + r[26:] if r.endswith('+00:00'): r = r[:-6] + 'Z' return r elif isinstance(o, datetime.date): return o.isoformat() elif isinstance(o, datetime.time): if is_aware(o): raise ValueError("JSON can't represent timezone-aware times.") r = o.isoformat() if o.microsecond: r = r[:12] return r elif isinstance(o, decimal.Decimal): return str(o) else: return super(DjangoJSONEncoder, self).default(o) 但是写Django的人很聪明。如果这是真正的解决办法,他们可能已经这么做了。为什么他们没有这么做呢?对unicode的调用,再加上JSON编码,会使DjangoJSONEncoder的修复错误吗


我甚至在Django文档中找到了一个,所以它不是未知的,我只是不明白为什么它不是默认值。

为什么不在尝试编码它之前解决它?没有明显的方法来解析
代理
实例,因为
代理
的实现只是一个
过程
。因为我没有尝试对它进行编码,其他东西都是,代理对象被隐藏在另一个对象中,可能还有另一个对象。。。。。 from django.utils.functional import Promise class DjangoJSONEncoder(json.JSONEncoder): """ JSONEncoder subclass that knows how to encode date/time and decimal types. """ def default(self, o): # See "Date Time String Format" in the ECMA-262 specification. if isinstance(o, datetime.datetime): r = o.isoformat() if o.microsecond: r = r[:23] + r[26:] if r.endswith('+00:00'): r = r[:-6] + 'Z' return r elif isinstance(o, datetime.date): return o.isoformat() elif isinstance(o, datetime.time): if is_aware(o): raise ValueError("JSON can't represent timezone-aware times.") r = o.isoformat() if o.microsecond: r = r[:12] return r elif isinstance(o, decimal.Decimal): return str(o) elif isinstance(o, Promise): # Added these lines here return unicode(o) else: return super(DjangoJSONEncoder, self).default(o)