【问题标题】:How can I relation countries and invoices for print a custom text?如何关联国家和发票以打印自定义文本?
【发布时间】:2021-08-01 17:02:47
【问题描述】:

在发票视图中,我有一个自定义布尔字段和一个自定义文本字段。

 Odoo Server Error
Traceback (most recent call last):
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/qweb.py", line 348, in _compiled_fn
    return compiled(self, append, new, options, log)
  File "<template>", line 1, in template_account_report_invoice_document_with_payments_104
  File "<template>", line 2, in body_call_content_103
  File "/home/ubuntu/OdooAddons/odoo/addons-extra/emb_account/models/account_invoice.py", line 13, in country_group_country_ids
    return self.env['country.group'].search([('name', '=', group)]).mapped('country_ids.id')
  File "/home/ubuntu/OdooAddons/odoo/odoo/api.py", line 831, in __getitem__
    return self.registry[model_name]._browse((), self)
  File "/home/ubuntu/OdooAddons/odoo/odoo/modules/registry.py", line 177, in __getitem__
    return self.models[model_name]
KeyError: 'country.group'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/OdooAddons/odoo/addons/web/controllers/main.py", line 1686, in report_download
    response = self.report_routes(reportname, docids=docids, converter=converter)
  File "/home/ubuntu/OdooAddons/odoo/odoo/http.py", line 519, in response_wrap
    response = f(*args, **kw)
  File "/home/ubuntu/OdooAddons/odoo/addons/web/controllers/main.py", line 1627, in report_routes
    pdf = report.with_context(context).render_qweb_pdf(docids, data=data)[0]
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/ir_actions_report.py", line 727, in render_qweb_pdf
    html = self.with_context(context).render_qweb_html(res_ids, data=data)[0]
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/ir_actions_report.py", line 767, in render_qweb_html
    return self.render_template(self.report_name, data), 'html'
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/ir_actions_report.py", line 540, in render_template
    return view_obj.render_template(template, values)
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/ir_ui_view.py", line 1338, in render_template
    return self.browse(self.get_view_id(template)).render(values, engine)
  File "/home/ubuntu/OdooAddons/odoo/addons/web_editor/models/ir_ui_view.py", line 29, in render
    return super(IrUiView, self).render(values=values, engine=engine, minimal_qcontext=minimal_qcontext)
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/ir_ui_view.py", line 1347, in render
    return self.env[engine].render(self.id, qcontext)
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/ir_qweb.py", line 59, in render
    result = super(IrQWeb, self).render(id_or_xml_id, values=values, **context)
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/qweb.py", line 275, in render
    self.compile(template, options)(self, body.append, values or {})
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/qweb.py", line 350, in _compiled_fn
    raise e
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/qweb.py", line 348, in _compiled_fn
    return compiled(self, append, new, options, log)
  File "<template>", line 1, in template_967_50
  File "<template>", line 2, in body_call_content_49
  File "<template>", line 3, in foreach_48
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/qweb.py", line 355, in _compiled_fn
    raise QWebException("Error to render compiling AST", e, path, node and etree.tostring(node[0], encoding='unicode'), name)
odoo.addons.base.models.qweb.QWebException: 'country.group'
Traceback (most recent call last):
  File "/home/ubuntu/OdooAddons/odoo/odoo/addons/base/models/qweb.py", line 348, in _compiled_fn
    return compiled(self, append, new, options, log)
  File "<template>", line 1, in template_account_report_invoice_document_with_payments_104
  File "<template>", line 2, in body_call_content_103
  File "/home/ubuntu/OdooAddons/odoo/addons-extra/emb_account/models/account_invoice.py", line 13, in country_group_country_ids
    return self.env['country.group'].search([('name', '=', group)]).mapped('country_ids.id')
  File "/home/ubuntu/OdooAddons/odoo/odoo/api.py", line 831, in __getitem__
    return self.registry[model_name]._browse((), self)
  File "/home/ubuntu/OdooAddons/odoo/odoo/modules/registry.py", line 177, in __getitem__
    return self.models[model_name]
KeyError: 'country.group'

Error to render compiling AST
KeyError: 'country.group'
Template: account.report_invoice_document_with_payments
Path: /templates/t/t/div/t[2]
Node: <t t-set="is_for_emb_message_country" t-value="o.partner_id.country_id and o.partner_id.country_id.id in o.country_group_country_ids('Exportaciones')"/>
            

我正在尝试在报告中打印我的自定义字段的文本,但前提是发票将发送到 国家/地区组,我有它的 ID。 我该如何做这个功能? 我如何关联国家和发票来创建它?

【问题讨论】:

  • 请提供您的代码或您为此尝试的内容。
  • 我没有代码。我不知道如何将国家/地区组与发票联系起来。我需要这种连接,因为如果发票发往国家/地区组的一个国家/地区,我需要在报告中显示一条消息
  • 用例子描述更多问题。
  • stackoverflow.com/questions/67499723/…这是我能给出的最好解释
  • 抱歉这个问题

标签: python xml odoo helper


【解决方案1】:

最简单的方法是在帐户发票模型中添加一个函数并从 qweb 报告中调用它。您可以定义 qweb report parser 并在那里定义它,以便在报告上下文中可用。

示例

我想国家组的定义如下:

class CountryGroup(models.Model):
    _name = 'country.group'

    name = fields.Char()
    country_ids = fields.Many2many("res.country")

您只需在帐户发票模型中定义一个函数以返回特定国家/地区组的国家/地区 ID:

class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    def country_group_country_ids(self, group):
        return self.env['country.group'].search([('name', '=', group)]).mapped('country_ids.id')

以下代码扩展了发票报告并在发票状态之后显示参考字段以进行演示:

<template id="report_invoice_document" inherit_id="account.report_invoice_document">
    <xpath expr="//h2" position="after">
        <t t-if="o.partner_id.country_id and o.partner_id.country_id.id in o.country_group_country_ids('export')">
            <p t-field="o.reference"/>
        </t>
    </xpath>
</template>

【讨论】:

  • 我用错误代码更新了问题
  • class CountryGroup(models.Model): _name = 'country.group' name = fields.Char() country_ids = fields.Many2many("res.country") x_export_message= fields.Text(string= "导出消息") ```
  • class AccountInvoice(models.Model): _inherit = 'account.invoice' x_message=fields.Text(string="Export Message" (与 CountryGroup x_export_message 相关)
  • 这是我从你的代码中得到的改变
  • 我有那个自定义字段,我需要将 x_export_message 与发票 x_message 关联
猜你喜欢
  • 2020-03-27
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多