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

Odoo发票报表关联国家组显示自定义字段及country.group报错处理

问题根因

报错的核心原因是你调用的模型名称有误,Odoo原生国家组模型的技术名为res.country.group,而非你代码中写的country.group,所以系统找不到对应模型触发KeyError。

完整实现方案

1. 模块依赖配置

首先确保你的自定义模块emb_account的__manifest__.py中已添加base模块依赖,国家组模型由base模块提供:

{
    # 其他配置项省略
    'depends': ['base', 'account'],
}

2. 发票模型自定义逻辑

在account_invoice.py中添加对应方法和自定义字段(Odoo13及以上版本发票模型为account.move,12及以下为account.invoice,按需调整继承的模型名即可):

from odoo import models, fields

class AccountInvoice(models.Model):
    # Odoo13+ 替换为 _inherit = 'account.move'
    _inherit = 'account.invoice'

    # 你自定义的两个字段
    custom_boolean_field = fields.Boolean(string="自定义布尔字段")
    custom_text_field = fields.Text(string="自定义文本字段")

    def check_partner_in_country_group(self, group_name):
        """校验当前发票的收件方国家是否属于指定名称的国家组"""
        self.ensure_one()
        # 无收件方/无收件方国家信息时直接返回False
        if not self.partner_id or not self.partner_id.country_id:
            return False
        # 用正确的模型名查找国家组
        target_group = self.env['res.country.group'].search([('name', '=', group_name)], limit=1)
        if not target_group:
            return False
        return self.partner_id.country_id.id in target_group.country_ids.ids

3. QWeb模板调整

修改报表模板中的调用逻辑,判断满足条件后打印自定义文本:

<!-- 先判断是否符合国家组要求 -->
<t t-set="meet_country_condition" t-value="o.check_partner_in_country_group('Exportaciones')"/>
<!-- 国家组条件和自定义布尔字段都为True时打印文本 -->
<t t-if="meet_country_condition and o.custom_boolean_field">
    <div class="custom-text-block">
        <span t-esc="o.custom_text_field"/>
    </div>
</t>

可选优化建议

不建议硬编码国家组名称,可提前为目标国家组定义XML ID,通过env.ref调用,避免国家组名称修改后逻辑失效:

# 假设你提前创建的国家组XML ID为 emb_account.export_country_group
target_group = self.env.ref('emb_account.export_country_group', raise_if_not_found=False)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 11:15:03