【问题标题】:Odoo 10 Custom Qweb report : Error to render compiling AST TypeError: 'NoneType' object is not callableOdoo 10 自定义 Qweb 报告:渲染编译 AST TypeError 时出错:“NoneType”对象不可调用
【发布时间】:2020-03-01 14:54:53
【问题描述】:

我正在尝试从供应商模块扩展现有报告,插入转换为文本的金额,因此我从头开始为我的报告创建模块:

这是我的 report_file.py

from openerp import api, models
from num2words import num2words

class ReportReceiptReprint(models.AbstractModel):
    _name = 'report.aces_pos_reorder.receipt_reprint'

@api.multi
def _numwords(val):
    pretext = val
    text = ''
    entire_part = int((str(pretext).split('.'))[0])
    decimal_part = int((str(pretext).split('.'))[1])  
    text+=num2words(entire_part, lang='es').upper()
    text+=' CON '
    text+=num2words(decimal_part, lang='es').upper()
    if decimal_num > 1:
        text+= ' CENTAVOS '
    else:
        text+= ' CENTAVO '
    return text

@api.multi
def render_html(self, docids, data=None):
    report = self.env['report']._get_report_from_name('aces_pos_reorder.receipt_reprint')
    docargs = {
        'doc_ids': docids,
        'doc_model': report.model,
        'docs': self,
        'num_words': _numwords,
        }
    return self.env['report'].render('aces_pos_reorder.receipt_reprint', docargs)

还有我的 report_file.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
   <template id="report_receipt_reprint_inherit" inherit_id="aces_pos_reorder.receipt_reprint">
     <xpath expr="//div[@class='numbers_text']" position="replace">
        <div style="font-size: 14px; width: 100%">
          <b>Son: <span t-esc="num_words(receipt.amount_total)"/></b>
        </div>
     </xpath>
   </template>
</data>
</odoo>

但我只是得到这个:

Error to render compiling AST
TypeError: 'NoneType' object is not callable
Template: 1130
Path: /templates/t/t/div[2]/div[9]/div[1]/b/span
Node: <span t-esc="num_words(receipt.amount_total)"/>

即使我将函数 _numwords() 放入主模块的 models.py 文件中,也会发生这种情况。 如果我避免调用该函数,则模块的扩展有效,文本按预期插入。 num2words 已安装。

如有任何意见或建议,我将不胜感激! 提前致谢。

==EDIT==

report_file.py

from odoo import api, models
from num2words import num2words


class ReportReceiptReprint(models.AbstractModel):
      _name = 'report.aces_pos_reorder.receipt_reprint'

@api.multi
def _numwords(self, val):
    pretext = float(val)
    text = ''
    entire_part = int((str(pretext).split('.'))[0])
    decimal_part = (pretext-float(entire_part))*100
    decimal_part = int((str(decimal_part).split('.'))[0])
    text += num2words(entire_part, lang='es').upper()
    text += ' LEMPIRAS CON '
    text += num2words(decimal_part, lang='es').upper()
    if decimal_part > 1:
        text += ' CENTAVOS '
    else:
        text += ' CENTAVO '
    return text

@api.multi
def render_html(self, docids, data=None):
    # report = self.env['report']._get_report_from_name('aces_pos_reorder.receipt_reprint')
    docargs = {
        'doc_ids': docids,
        'doc_model': self.env.context.get('active_model'),
        'docs': self.env['pos.order'].browse(int(docids)),
        'num_words': self._numbwords,
    }
    return self.env['report'].render('aces_pos_reorder.receipt_reprint', docargs)

我不知道为什么注释行“#report =”会导致很多问题。

【问题讨论】:

  • 也许 self.numwords 也可以尝试使用大括号和参数
  • 感谢您的评论 @MuhammadYusuf ,但我更改了如下代码:` 'num_words': self._numwords(val), ` 但仍然收到相同的消息。

标签: report odoo-10 qweb


【解决方案1】:

在您的 python 文件中,只需将函数调用更改为,

'num_words': self._numwords(data['form']['amount_total']), & def _numwords(self, val)

请务必检查您传递的 amount_total 值是否来自您的数据,并且该值必须传递给 _numwords 函数。 并在 XML 文件 t-esc="num_words" 尝试像这样访问值。

谢谢

【讨论】:

  • 感谢@DipenShah,但未返回值,显示报告但没有预期值。
  • 您好,@RauLeo88 您想将 amount_total 转换为文本对吗?然后在您的代码上,您将返回您的 amount_total(10) 以 (TEN) 返回的文本。因此,当您之前检查我的解释并尝试根据您获得的数据进行打印时?
  • 嗨@Dipen-Shah,是的,我希望得到这个词而不是数字,但是在你的代码之后,我只是在'Son:'标签之后得到空格,** t-esc = num_words" ** 什么都不返回。
  • 您好,@RauLeo88 好的,您可以在打印时检查 data['form']['amount_total'] 得到什么。您还可以使用 def _numwords 函数跟踪它们返回的内容吗?
  • 嗨,@Dipen-Shah,该函数的跟踪返回预期的值(文本),_numwords(123.25)'CIENTO VEINTITRÉS CON VEINTICINCO CENTAVOS',但打印表单值返回:错误到渲染编译 AST 类型错误:列表索引必须是整数,而不是 str 模板:1130 路径:/templates/t/t/div[2]/div[9]/div[1]/b/span 节点:
【解决方案2】:
@api.milti
def _numbwords(self, val)....

@api.multi
def render_html(self, docids, data=None):
# report = self.env['report']._get_report_from_name('aces_pos_reorder.receipt_reprint')
# This line caused problems so commented out
docargs = {
    'doc_ids': docids,
    'doc_model': self.env.context.get('active_model'),
    'docs': self.env['pos.order'].browse(int(docids)), # have to include the model and methods to index the documents
    'num_words': self._numbwords,
}
return self.env['report'].render('aces_pos_reorder.receipt_reprint', docargs)

就是这样

【讨论】:

    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多