【发布时间】: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), ` 但仍然收到相同的消息。