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

时间:2017-03-27 02:38:31

标签: python openerp odoo-8 odoo-10

考虑这个功能:

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

我认为这个功能可以比现在容易得多(代码少),我只是想尊重原来的功能。

我怎样才能在"纯粹"新API(来自Odoo v10社区)?

1 个答案:

答案 0 :(得分:1)

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

这有两个原因:

  1. 您正在使用ensure_one,因此无需循环
  2. 你正在循环一个空记录集(你没有浏览任何东西)
  3. 你应该这样做:

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

    self已经是你的向导;)