Skip to Content
Menu
This question has been flagged
3 Replies
3526 Views

Hello,


In Odoo 12, I created a custom module which will override the MRP Production report. In the report, I added a python function which is in model mrp.production. it returns a list of dictionaries. I printed to the output and verified that it returns a single list of dictionaries.


I set the variable in the report using this

<t t-set="lines" t-value="o.get_sorted_lines()"/>


I then iterate over the lines using

<t t-foreach="lines" t-as="test_line">


When I call

<span t-esc="line['product_name']"/>

I receive an error that list must be indices not str. I then alter the output to show the dictionary.

<span t-esc="line"/>

It generates the report but each line contains the list of dicts.


I don't get it. It seems like a bug to me. Calling a function which returns a valid iterable, then try to iterate but it doesn't iterate and just outputs the object. Anyone else had a similar experience?

Avatar
Discard
Best Answer

It's not a bug but rather a technical mistake in the code.

Try the following code:

<t t-set="lines" t-value="o.get_sorted_lines()"/>

<t t-foreach="lines" t-as="test_line">
<span t-esc="test_line['product_name']"/>
</t>

This code will work only if the method returns a list of dict.
Ex: [{'product_name': 'A'}, {'product_name': 'B'}]

Avatar
Discard
Best Answer

There seems to be a typo in the example provided. The variable is called "test_line"  but it is being referenced as "line" in the t-esc. Also, try referencing the field using the dot notation as line.product_name

Avatar
Discard
Author Best Answer

Not allowed to post comments to my own question. I did not paste the code exactly how I had it. After reviewing the answers, I created a module and tested fresh and this issue still seems valid. My Odoo version id 12.0 with git log showing

commit c9bedcc0efc71d71e26410db2ec1db1f4009a0b6 (HEAD -> 12.0, origin/12.0)
Author: Andrea Grazioso (agr-odoo) <agr@odoo.com>
Date:   Fri Nov 8 09:39:40 2019 +0000

I also committed the test module to git
https://github.com/aliomattux/mrp_production_report


Error below. To me definitely seems like bug. I moved the entire module over to dictionary from custom function with identical template and it works. Was the only way I could get it to work.


Traceback (most recent call last):
  File "/opt/odoo/odoo/addons/web/controllers/main.py", line 1671, in report_download
    response = self.report_routes(reportname, docids=docids, converter=converter)
  File "/opt/odoo/odoo/odoo/http.py", line 519, in response_wrap
    response = f(*args, **kw)
  File "/opt/odoo/odoo/addons/web/controllers/main.py", line 1612, in report_routes
    pdf = report.with_context(context).render_qweb_pdf(docids, data=data)[0]
  File "/opt/odoo/odoo/odoo/addons/base/models/ir_actions_report.py", line 711, in render_qweb_pdf
    html = self.with_context(context).render_qweb_html(res_ids, data=data)[0]
  File "/opt/odoo/odoo/odoo/addons/base/models/ir_actions_report.py", line 751, in render_qweb_html
    return self.render_template(self.report_name, data), 'html'
  File "/opt/odoo/odoo/odoo/addons/base/models/ir_actions_report.py", line 534, in render_template
    return view_obj.render_template(template, values)
  File "/opt/odoo/odoo/odoo/addons/base/models/ir_ui_view.py", line 1304, in render_template
    return self.browse(self.get_view_id(template)).render(values, engine)
  File "/opt/odoo/odoo/addons/web_editor/models/ir_ui_view.py", line 27, in render
    return super(IrUiView, self).render(values=values, engine=engine, minimal_qcontext=minimal_qcontext)
  File "/opt/odoo/odoo/odoo/addons/base/models/ir_ui_view.py", line 1313, in render
    return self.env[engine].render(self.id, qcontext)
  File "/opt/odoo/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 "/opt/odoo/odoo/odoo/addons/base/models/qweb.py", line 274, in render
    self.compile(template, options)(self, body.append, values or {})
  File "/opt/odoo/odoo/odoo/addons/base/models/qweb.py", line 353, 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: list indices must be integers or slices, not str
Traceback (most recent call last):
  File "/opt/odoo/odoo/odoo/addons/base/models/qweb.py", line 346, in _compiled_fn
    return compiled(self, append, new, options, log)
  File "<template>", line 1, in template_950_68
  File "<template>", line 2, in body_call_content_67
  File "<template>", line 3, in foreach_66
  File "<template>", line 4, in body_call_content_65
  File "<template>", line 96, in foreach_64
TypeError: list indices must be integers or slices, not str

Error to render compiling AST
TypeError: list indices must be integers or slices, not str
Template: 950
Path: /templates/t/t/t/t/div/table/tbody/t/t[2]/tr/td/span
Node: <span t-esc="line['sku']"/>

I teste

Avatar
Discard