如何从 Odoo v10 中删除此控制器错误
Posted
技术标签:
【中文标题】如何从 Odoo v10 中删除此控制器错误【英文标题】:How to remove this controller error from Odoo v10 【发布时间】:2019-09-24 10:14:17 【问题描述】:我编写了一个控制器来将数据推送到客户端。代码如下。有人可以提一下这有什么错误吗?因为我遇到了类似
的错误File "/home/user/odoo/odoo10/custom/project/project_custom/controllers/main.py", line 21, in post_api_method
partners = request['import.order'].sudo().search_read([('customer', '=', 'TRADING COMPANY')])
File "/home/user/.local/lib/python2.7/site-packages/werkzeug/local.py", line 373, in <lambda>
__getitem__ = lambda x, i: x._get_current_object()[i]
TypeError: 'HttpRequest' object has no attribute '__getitem__'
我的代码就在下面:
# -*- coding: utf-8 -*-
from odoo import http
import requests
import json
from odoo.http import request
from odoo import report as odoo_report
import datetime
import pytz
import base64
import logging
_logger = logging.getLogger(__name__)
url = 'https://*********/api/********logistics/fillshipmentdetails***'
authentication = '129487521452XSDFG156235C6B353CD75QWERBVFGHJAA96AA99EDSJUITH7AAF65258F22871E7D415EB4A77CF7B4CB513A993680FC6053EAE6RT785YUIJ124'
class MyApi(http.Controller):
@http.route("/my/post/api/path", url=url, auth="none", type='http', method=['POST'], csrf=False, cors='*')
def post_api_method(self, *kw):
partners = request['import.order'].sudo().search_read([('customer', '=', 'TRADING COMPANY')])
headers = 'Content-Type': 'application/json'
body = 'results': "shipmentType": partners.ship_Type,
"shipperName": partners.customer.name,
"consigneeName": "*****",
"vslexparvlDate": "2019-03-05 05:55:13:000",
"vsldischlDate": "2019-04-02 05:55:13:000",
"docRecvByMailDate": "2019-03-05 05:55:13:000",
"orgDocRecvgDate": "2019-03-24 05:55:13:000",
"vesselArvlDate": "2019-03-19 05:55:13:000",
"bLAwbNumber": "*****",
"houseBL": "****",
"dischargingPort": "*****",
"landingPort": "*****",
"containerNumber": "*****",
"delOrderDate": "2019-03-29 05:55:13:000",
"delOrderNumber": "******",
"bayanNumber": "*****",
"bayanDate": "2019-03-30 05:55:13:000",
"containerType": "*****",
"dutyPaidDate": "2019-03-22 05:55:13:000",
"transportNTFTerminal": "***",
"transportDirCustomer": "***",
"demmurageDate": "2019-03-27 05:55:13:000",
"lastDetentionDate": "2019-03-27 05:55:13:000",
"eIRReturnToShipLine": "****",
"eventUpdateStatus": "Delivered"
response = requests.post(url, json.dumps(body), headers=headers)
return response
所有给键的值都是演示数据。只有前两个键值是正确的。
【问题讨论】:
【参考方案1】:您可以使用绑定到当前请求 (http.request.env
) 的环境访问 ORM 模型。
partners = request.env['import.order'].sudo().search_read([('customer', '=', 'TRADING COMPANY')])
【讨论】:
以上是关于如何从 Odoo v10 中删除此控制器错误的主要内容,如果未能解决你的问题,请参考以下文章
如何在typo3 V10的extbase控制器中重定向到404页面
如何使用 type='json' 在 Odoo 控制器中获取 JSON 数据?