使用odoo v11与restapi集成虽然在获取错误的报告数据时在CRUD操作中正常工作
file.py
def generate_report(self, xml_id, ids):
self_reports = {}
self_reports = {'result': False, 'state': False, 'exception': None}
try:
result, format = request.env.ref(xml_id).sudo().render_qweb_pdf([ids])
if not result:
tb = sys.exc_info()
self_reports['exception'] = odoo.exceptions.DeferredException('RML is not available at specified location or not enough data to print!', tb)
self_reports['result'] = result
self_reports['format'] = format
self_reports['state'] = True
self_reports.update({'id': ids})
except Exception as exception:
_logger.exception('Exception: %s\n', exception)
if hasattr(exception, 'name') and hasattr(exception, 'value'):
self_reports['exception'] = odoo.exceptions.DeferredException(tools.ustr(exception.name), tools.ustr(exception.value))
else:
tb = sys.exc_info()
self_reports['exception'] = odoo.exceptions.DeferredException(tools.exception_to_unicode(exception), tb)
self_reports['state'] = True
exc = self_reports['exception']
if exc:
raise UserError('%s: %s' % (exc.message, exc.traceback))
if self_reports['state']:
if tools.config['reportgz']:
import zlib
res2 = zlib.compress(result)
else:
if isinstance(result, str):
res2 = result.encode('latin1', 'replace')
else:
res2 = result
if res2:
self_reports['result'] = base64.encodestring(res2)
return self_reports
def get_response(self, status_code, status, data=None):
"""Returns Response Object with given status code and status"""
response = Response()
response.status = status
if data:
response.data = isinstance(data, str) and data or json.dumps(data)
response.status_code = status_code
return response
这是数据格式
list: [{'exception': None, 'state': True, 'id': 3, 'format': 'pdf', 'result':
b'SlZCRVJpMHhMak1LTVNBd0lHOWlhZ284UEFvdlZIbHdaU0F2VUdGblpYTUtMME52ZFc1MElERUtM\nMHRwWkh
NZ1d5QXpJREFnVWlCZApDajQrQ21WdVpHOWlhZ295SURBZ2IySnFDanc4Q2k5UWNtOWtk\nV05sY2lBb1VIbFFSR
Vl5S1FvK1BncGxibVJ2WW1vS015QXdJRzlpCmFnbzhQQW92VW1WemIzVnlZ\nMlZ6SURVZ01DQlNDaTlCYm01dmR
ITWdNVGdnTUNCU0NpOVFZWEpsYm5RZ01TQXdJRklLTDFSNWNH\nVWcKTDFCaFoyVUtMMDFsWkdsaFFtOTRJR
nNnTUNBd0lEWXhNaUEzT1RJZ1hRb3ZRMjl1ZEdWdWRI\nTWdNVGtnTUNCU0NqNCtDbVZ1Wkc5aQphZ28wSUR
BZ2IySnFDanc4Q2k5VWVYQmxJQzlEWVhSaGJH\nOW5DaTlRWVdkbGN5QXhJREFnVWdvK1BncGxibVJ2WW1vS05T
QXdJRzlpCmFnbzhQQW92UTI5c2Iz\nSlRjR0ZqWlNBOFBBb3ZRMU53SUM5RVpYWnBZMlZTUjBJS0wwTlRjR2NnTD
BSbGRtbGpaVWR5WVhr\nS0wxQkQKVTNBZ05pQXdJRklLUGo0S0wwVjRkRWRUZEdGMFpTQThQQW92UjFOaElEY
2dNQ0JTQ2o0\nK0NpOUdiMjUwSUR3OENpOUdPQ0E0SURBZwpVZ292UmpjZ01UTWdNQ0JTQ2o0K0NpOVFZWFI ...
错误追溯:
File "E:\Odoo\odoo11\addons\restapi\controllers\main.py", line 343, in call_report
return self.get_response(200, str(200), {'report': datas})
File "E:\Odoo\odoo11\addons\restapi\controllers\main.py", line 135, in get_response
response.data = isinstance(data, str) and data or json.dumps(data)
File "C:\Program Files\Python\Python35\lib\json\__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "C:\Program Files\Python\Python35\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Program Files\Python\Python35\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Program Files\Python\Python35\lib\json\encoder.py", line 180, in default
raise TypeError(repr(o) + " is not JSON serializable")
我需要在api响应中使用二进制数据的pdf报告,在响应数据中获取错误。请有人帮我解决这个问题
答案 0 :(得分:0)
这是因为您可能忘记导入json lib。在您的标头声明"title": "Living furniture exhibitions!"
"include_products": ["ikea chair", "marketO desk"]
中,这可能会帮助您解决错误。