如何在写入函数 Odoo 14 中调用向导

问题描述

在'stock.move'模型的写入函数中,如果某些条件,我不想返回向导,但不起作用。

这是我的代码

 def write(self,vals):

        # Handle the write on the initial demand by updating the reserved quantity and logging
        # messages according to the state of the stock.move records.
        receipt_moves_to_reassign = self.env['stock.move']
        if 'product_uom_qty' in vals:
            for move in self.filtered(lambda m: m.state not in ('done','draft') and m.picking_id):
                if float_compare(vals['product_uom_qty'],move.product_uom_qty,precision_rounding=move.product_uom.rounding):
                    self.env['stock.move.line']._log_message(move.picking_id,move,'stock.track_move_template',vals)
            if self.env.context.get('do_not_unreserve') is None:
                move_to_unreserve = self.filtered(
                    lambda m: m.state not in ['draft','done','cancel'] and float_compare(m.reserved_availability,vals.get('product_uom_qty'),precision_rounding=m.product_uom.rounding) == 1
                )
                move_to_unreserve._do_unreserve()
                (self - move_to_unreserve).filtered(lambda m: m.state == 'assigned').write(
                    {'state': 'partially_available'})
                # When editing the initial demand,directly run again action assign on receipt moves.
                receipt_moves_to_reassign |= move_to_unreserve.filtered(lambda m: m.location_id.usage == 'supplier')
                receipt_moves_to_reassign |= (self - move_to_unreserve).filtered(
                    lambda m: m.location_id.usage == 'supplier' and m.state in ('partially_available','assigned'))
        if 'date_deadline' in vals:
            self._set_date_deadline(vals.get('date_deadline'))
        res = super(StockMove,self).write(vals)
        if 'move_line_nosuggest_ids' in vals:
            done_qty = vals['move_line_nosuggest_ids'][0][2]['qty_done']
            for rec in self:
                if rec.product_uom_qty < done_qty:
                    return self.env["ir.actions.actions"]._for_xml_id("my_module.action_test")


        if receipt_moves_to_reassign:
            receipt_moves_to_reassign._action_assign()
        return res

请问怎么了? 有什么帮助吗? 谢谢。

解决方法

您无法打开向导或写入任何操作。如果你看到 write 函数的返回类型,它是一个布尔值

Thumb 规则:在浏览器请求/调用之前,您不能返回操作

您可以尝试的解决方法是,通过消息引发 ValidationError 或 UserError。