亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 JSON 控制器調用 Odoo 數據?

如何從 JSON 控制器調用 Odoo 數據?

RISEBY 2023-06-27 16:42:16
所以我需要創建一個 Rest 服務,它將調用特定模型中的數據。為了做到這一點,我必須創建一個控制器。我是 Odoo 和 Json 的新手,我找不到任何可以解決這個問題的東西。到目前為止,除了 http 請求之外,我所需要的就是我所需要的。class RestService(http.Controller):    @http.route('/orders/<model("sale.order"):order>', auth='none', website=True)    def consulta_orden(self , order):        return request.render('consulta_pedidos.order', {'order': order})                return {'order_id': order_id}這個想法是,當我加載數據時,我可以在模板中呈現這些數據。已經用 Http 請求做到了這一點,但我需要用 Json 來做到這一點。有關如何更好地實施它的任何提示。
查看完整描述

2 回答

?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

我用一種非常簡單的方式解決了我的問題。


代碼:


@http.route(['/orden_detalle', '/orden_detalle/<int:order_id>'], type='json', auth='user')

? ? def orden_detalle(self, order_id=None):

? ? ? ? if order_id:

? ? ? ? ? ? domain = [('id', '=', order_id)]

? ? ? ? else:

? ? ? ? ? ? domain = []

? ? ? ? sales_rec = request.env['sale.order'].search(domain)

? ? ? ? sales = []

? ? ? ? for rec in sales_rec:

? ? ? ? ? ? vals = {

? ? ? ? ? ? ? ? 'id': rec.id,

? ? ? ? ? ? ? ? 'name': rec.name,

? ? ? ? ? ? ? ? 'partner_id': rec.partner_id.name,

? ? ? ? ? ? ? ? 'user_id': rec.user_id.name,

? ? ? ? ? ? }

? ? ? ? ? ? sales.append(vals)

? ? ? ? data = {'status': 200, 'response': sales, 'message': 'Sale(s) returned'}

? ? ? ? return data

查看完整回答
反對 回復 2023-06-27
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

如果您想返回 Json(對于 REST-API),您只需在 Response 對象中返回它即可。

確保您已從Response導入odoo.http。然后你可以像這樣返回json數據:

return Response(json.dumps({'order_id': order_id}), status=200, content_type="application/json")

我通常將此邏輯包裝在一個單獨的函數中(在控制器外部),該函數在更新的 Odoo 對象上記錄請求和響應,然后返回 Response 對象。這樣,它也可以在錯誤處理中輕松重用(然后您可以在其中返回 Json 格式的自定義錯誤響應)

編輯:還要確保您沒有包含Content-Type: application/json在請求中,在這種情況下,Odoo 會認為這是一個 JSON-RPC 請求。


查看完整回答
反對 回復 2023-06-27
  • 2 回答
  • 0 關注
  • 254 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號