0

In the invoice view I have a custom boolean field and a custom text field.

 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')"/>
            

I'm trying to print in the report the text of my custom field but only if the invoice is going to go to a country group i have the id of it. How can I do the function? How can I relation countries and invoices to create this?

  • Pls your code or what you have attempted for this point. – Dipen Shah May 11 '21 at 13:32
  • I don't have code. I don't know how to connect the country groups with the invoices. I need that connection because if the invoice goes to one country of the country group I need to appear a message in the report –  May 11 '21 at 13:42
  • Describe more about problem with example. – Bhoomi Vaishnani May 11 '21 at 20:48
  • https://stackoverflow.com/questions/67499723/how-can-i-create-this-function-in-odoo-12-or-openerp this is the best explanation that i can give –  May 12 '21 at 08:37
  • sorry for this question –  May 12 '21 at 08:37
  • `country.group` is not loaded into the database, check if you added the file to the `__init__` file and make sure to update the module. – Kenly May 16 '21 at 08:35

1 Answers1

1

The easiest way is to add a function in account invoice model and call it from the qweb report. You can define a qweb report parser and define it there so it will be available in the report context.

Example

I suppose that the country group is defined like following:

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

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

All you need is to define a function in account invoice model to return the country ids of a specific country group:

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')

The following code extend the invoice report and show the reference field just after the invoice status for demonstration:

<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>
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • I updated the question with the error code –  May 12 '21 at 13:26
  • class CountryGroup(models.Model): _name = 'country.group' name = fields.Char() country_ids = fields.Many2many("res.country") x_export_message= fields.Text(string="Export Message") ``` –  May 12 '21 at 13:49
  • class AccountInvoice(models.Model): _inherit = 'account.invoice' x_message=fields.Text(string="Export Message" (related with CountryGroup x_export_message) –  May 12 '21 at 13:50
  • That are the changes I have from your code –  May 12 '21 at 13:50
  • I have that custom field and I need to relation x_export_message to invoice x_message –  May 12 '21 at 13:50
  • Thank you for that code but it's still not working –  May 13 '21 at 09:39
  • I tested the code, and it is working, please update your question and add what you have tried. – Kenly May 16 '21 at 07:45
  • There is the error, I didn't see your message –  May 26 '21 at 08:31
  • The error message tells you that the `emb_dual_use_country` field does not exist. Maybe the file where it is defined is not loaded, or it is defined in the wrong model. – Kenly May 26 '21 at 08:47
  • Here is the code model https://pastebin.com/JHN4H8tW of that –  May 26 '21 at 09:07
  • The code looks fine, make sure that file is imported in `__init__.py` file. – Kenly May 26 '21 at 09:15
  • Yes, all the `__init__.py` files are right –  May 26 '21 at 09:17
  • Do you still get the same error after upgrading your module? – Kenly May 26 '21 at 09:36
  • Did you upgrade the module? – Kenly May 27 '21 at 11:30