You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 16中如何在res.partner表单视图展示外部API返回的字典数据

实现步骤

1. 模型层:添加计算字段存储API数据

在自定义模块的models/res_partner.py中,继承res.partner模型并添加JSON类型的计算字段,用于存储外部API返回的字典数据:

from odoo import models, fields, api
import requests  # 若使用requests调用API,需在模块依赖中声明requests

class ResPartner(models.Model):
    _inherit = 'res.partner'

    api_data = fields.Json(string="External API Data", compute="_compute_api_data", store=False)

    @api.depends('id')  # 可根据业务需求调整依赖字段,比如partner的特定字段变更时重新拉取数据
    def _compute_api_data(self):
        for partner in self:
            # 调用你已实现的API获取方法
            partner.api_data = self._fetch_external_api_data()

    def _fetch_external_api_data(self):
        # 替换为你的实际JSON-RPC调用逻辑,示例用GET请求演示
        api_url = "https://your-external-api-endpoint"
        # 若为JSON-RPC请求,可参考Odoo内置方法:self.env['ir.http']._jsonrpc_request(...)
        response = requests.get(api_url)
        response.raise_for_status()  # 捕获API请求异常
        return response.json()

2. 视图层:在标签页中展示API数据

在自定义模块的views/res_partner_views.xml中,继承原表单视图,新增标签页并通过QWeb语法解析JSON字段:

<odoo>
    <record id="res_partner_form_api_inherit" model="ir.ui.view">
        <field name="name">res.partner.form.api.inherit</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form"/>
        <field name="arch" type="xml">
            <!-- 在现有标签页后新增API数据标签页 -->
            <xpath expr="//notebook/page[last()]" position="after">
                <page string="External API Data">
                    <!-- 展示单个key值:key1、key2 -->
                    <div class="oe_row">
                        <div class="oe_half">
                            <label string="Key 1:" class="oe_edit_only"/>
                            <field name="api_data" t-esc="record.api_data.value.get('key1', 'No data')" class="oe_read_only"/>
                        </div>
                        <div class="oe_half">
                            <label string="Key 2:" class="oe_edit_only"/>
                            <field name="api_data" t-esc="record.api_data.value.get('key2', 'No data')" class="oe_read_only"/>
                        </div>
                    </div>

                    <!-- 遍历字典中的列表数据 -->
                    <div class="oe_spacer"/>
                    <h4>API Data List</h4>
                    <table class="oe_list oe_list_content">
                        <thead>
                            <tr>
                                <th>Item Name</th>
                                <!-- 根据API返回字段添加对应表头 -->
                            </tr>
                        </thead>
                        <tbody>
                            <!-- 遍历api_data中的data_list字段,无数据时显示提示 -->
                            <tr t-foreach="record.api_data.value.get('data_list', [])" t-as="item">
                                <td t-esc="item.get('name', 'Unnamed')"/>
                                <!-- 其他字段示例:<td t-esc="item.get('field_name', 'N/A')"/> -->
                            </tr>
                            <tr t-if="not record.api_data.value.get('data_list')">
                                <td colspan="1" class="oe_center">No data available</td>
                            </tr>
                        </tbody>
                    </table>
                </page>
            </xpath>
        </field>
    </record>
</odoo>

3. 可选:添加手动刷新按钮

若需要手动触发API数据更新,可在模型中添加刷新方法,并在视图中添加按钮:

模型层新增方法

def action_refresh_api_data(self):
    self.ensure_one()
    # 强制重新计算api_data字段
    self._compute_api_data()

视图层添加按钮

在标签页顶部插入按钮:

<button name="action_refresh_api_data" string="Refresh API Data" type="object" class="oe_highlight" style="margin-bottom: 10px;"/>

注意事项

  • API权限与异常处理:确保Odoo服务器能访问外部API地址,在API调用方法中添加异常捕获(如网络错误、返回格式错误),避免页面崩溃。
  • 数据依赖:调整_compute_api_data中的@api.depends参数,确保在需要更新API数据时自动触发计算。
  • 数据结构适配:根据实际API返回的字典结构,修改视图中get('key1')、get('data_list')的键名,保证与API返回数据匹配。

内容的提问来源于stack exchange,提问作者Arthur Fortin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 14:32:34