鉴于以下模型:
class BaseModel(Model):
class Meta:
database = database
class ContentItem(BaseModel):
content = JSONField(index=True)
content_item_set = TextField(db_column='content_item_set_id')
id = TextField(primary_key=True)
original_content = TextField(null=True)
timestamp = DateTimeField()
class Meta:
db_table = 'content_item'
schema = 'cola'
当我尝试在Chalice视图中显示时,我有循环引用。
@app.route("/")
def list_items():
return [model_to_dict(i) for i in ContentItem.select().limit(10)]
错误:
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 317, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 348, in process_request
self.finish_request(request, client_address)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Users/jameslin/virtualenvs/cola-management-services/lib/python3.6/site-packages/chalice/local.py", line 514, in __init__
self, request, client_address, server) # type: ignore
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 696, in __init__
self.handle()
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/server.py", line 418, in handle
self.handle_one_request()
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/server.py", line 406, in handle_one_request
method()
File "/Users/jameslin/virtualenvs/cola-management-services/lib/python3.6/site-packages/chalice/local.py", line 535, in _generic_handle
body=body
File "/Users/jameslin/virtualenvs/cola-management-services/lib/python3.6/site-packages/chalice/local.py", line 468, in handle_request
response = self._app_object(lambda_event, lambda_context)
File "/Users/jameslin/virtualenvs/cola-management-services/lib/python3.6/site-packages/chalice/app.py", line 627, in __call__
response = response.to_dict(self.api.binary_types)
File "/Users/jameslin/virtualenvs/cola-management-services/lib/python3.6/site-packages/chalice/app.py", line 317, in to_dict
body = json.dumps(body, default=handle_decimals)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
ValueError: Circular reference detected
答案 0 :(得分:0)
可能是DateTimeField
。 Chalice JSON编码器不处理DateTimeField
(显然仅处理decimal
):https://github.com/aws/chalice/blob/master/chalice/app.py#L351
针对此类似问题的建议解决方法是在序列化之前先处理数据,然后将datetime
更改为另一种类型(可能是字符串表示形式):https://github.com/aws/chalice/issues/288#issuecomment-302896045 < / p>