簡體   English   中英

檢查某些字段是否已激活的功能-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

因此,我想功能是btw,它是從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社區)上實現此目標?

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已經是你的向導;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM