检查某些字段是否被激活的功能 - Odoo v8 到 Odoo v10 社区

Function to check if some fields are activated - Odoo v8 to Odoo v10 community

考虑这个功能:

@api.multi
def create_invoice(self):
    """ Create a invoice refund
    """

    self.ensure_one()
    wizard_brw = self.browse() 
    inv_id = self._context.get('active_id')
    for wizard in wizard_brw:
        if not wizard.sure:
            raise UserError(
                _("Validation error!"),
                _("Please confirm that you know what you're doing by"
                  " checking the option bellow!"))
        if (wizard.invoice_id and wizard.invoice_id.company_id.jour_id and
                wizard.invoice_id and wizard.invoice_id.company_id.acc_id):
            inv_id = self.action_invoice_create(wizard,
                                                wizard.invoice_id) 
        else:
            raise UserError(
                _('Validation error!'),
                _("You must go to the company form and configure a journal"
                  " and an account for damaged invoices"))
    return self.new_open_window([inv_id], 'action_invoice_tree1', 'account') 

除其他外,此按钮应检查 company_id 是否选择了 jour_id 字段,或 acc_id.

res.partner 上的这些字段是:

class ResCompany(models.Model):
_inherit = 'res.company'

jour_id = fields.Many2one('account.journal', string='Journal', required=False,
    help="Default journal for damaged invoices")
acc_id = fields.Many2one('account.account', string='Account', 
    help="Default account used for invoices and lines from damaged invoices")
printer_fiscal = fields.Boolean(string='Manages fiscal printer',
    help='Indicates that the company can operate a fiscal printer')

现在,这个函数没有显示任何警告或UserError,当然我已经导入了from odoo.exceptions import UserError

所以,我猜函数有问题,顺便说一句,这是从 Odoo v8 手动迁移的,它是本地化的。

原来的方法是这样的:

def create_invoice(self, cr, uid, ids, context=None):
    """ Create a invoice refund
    """
    context = context or {}
    wizard_brw = self.browse(cr, uid, ids, context=context)
    inv_id = context.get('active_id')
    for wizard in wizard_brw:
        if not wizard.sure:
            raise osv.except_osv(
                _("Validation error!"),
                _("Please confirm that you know what you're doing by"
                  " checking the option bellow!"))
        if (wizard.invoice_id and wizard.invoice_id.company_id.jour_id and
                wizard.invoice_id and wizard.invoice_id.company_id.acc_id):
            inv_id = self.action_invoice_create(cr, uid, ids, wizard,
                                                wizard.invoice_id, context)
        else:
            raise osv.except_osv(
                _('Validation error!'),
                _("You must go to the company form and configure a journal"
                  " and an account for damaged invoices"))
    return self.new_open_window(cr, uid, ids, [inv_id],
                                'action_invoice_tree1', 'account')

我认为这个功能可以比现在简单得多(代码更少),我只是尽量尊重原始功能。

如何在 "pure" new API(来自 Odoo v10 社区)上实现此目的?

self.ensure_one()
wizard_brw = self.browse() 
inv_id = self._context.get('active_id')
for wizard in wizard_brw:

这是错误的,原因有两个:

  1. 您正在使用 ensure_one,因此无需循环
  2. 您正在空记录集上循环(您没有浏览任何内容)

你应该这样做:

self.ensure_one()
if not self.sure:
     # do stuff

self 已经是你的向导 ;)