使用on函数计算两个字段

时间:2019-04-18 09:55:56

标签: odoo odoo-8

我有一个计算qty_incoming的函数,但是我想使用相同的函数计算出一个outgoing_qty字段,而不是为其计算创建单独的函数。我该怎么办?

 _columns = {
            '
            'incoming_qty': fields.function(_product_inc_out_qty, type='float',
                digits_compute=dp.get_precision('Product Unit of Measure'),
                string='Incoming'
            ),
            'outgoing_qty': fields.function(_product_inc_out_qty, type='float',
                digits_compute=dp.get_precision('Product Unit of Measure'),
                string='Outgoing'
            ),
        }

功能:

def _product_inc_out_qty(self, cr, uid, ids, field_names=None, arg=False, context=None):
        if context is None:
            context = {}

        res = {}
        for move_id in ids:
            move = self.browse(cr, uid, move_id, context=context)

            res[move.id] = move.product_id.incoming_qty or 0.0
        return res

如果我做这样的事情,那么我会报错TypeError:float()参数必须是字符串或数字

  def _product_inc_out_qty(self, cr, uid, ids, field_names=None, arg=False, context=None):
        if context is None:
            context = {}

        res = {}
        vals = {
            'outgoing_qty': 0.0,
            'incoming_qty': 0.0,


        }
        for move_id in ids:
            move = self.browse(cr, uid, move_id, context=context)
            vals['outgoing_qty'] = move.product_id.qty_available or 0.0
            vals['incoming_qty'] = move.product_id.incoming_qty or 0.0
            res[move.id] = vals

        return res

2 个答案:

答案 0 :(得分:1)

以相同的方法同时使用多个fields can be computed,只需对所有字段使用相同的方法并设置所有字段:

discount_value = fields.Float(compute='_apply_discount')
total = fields.Float(compute='_apply_discount')

@depends('value', 'discount')
def _apply_discount(self):
    for record in self:
        # compute actual discount from discount percentage
        discount = record.value * record.discount
        record.discount_value = discount
        record.total = record.value - discount

您可以在sale_order的旧api中找到示例

答案 1 :(得分:0)

我的代码中的问题是,在旧的API中,如果您想返回多个字段的值,则需要向字段中添加multi="any_string"

所以我的字段应该是这样

'incoming_qty': fields.function(_product_inc_out_qty, type='float',
                digits_compute=dp.get_precision('Product Unit of Measure'),
                multi='all',
                string='Incoming'
            ),