Odoo:如何覆盖原始函数

4
在Odoo中,每次打开产品表单时都会计算该产品的数量。这是在模型product.product ==> function _product_available中完成的。
该函数返回一个名为res的字典。
例如:
res = {8: {'qty_available': 5000.0, 'outgoing_qty': 1778.5, 'virtual_available': 3221.5, 'incoming_qty': 0.0}}

现在我想修改这些值。我已经通过直接在原始的function _product_available中进行编码来完成了这个操作。

由于这不是正确的方法,我想在继承模型中完成这个操作。我认为我需要覆盖function?或者重写?不确定它叫什么名字。

我读到的关于这样做的所有信息对我来说都很模糊。我找不到太多好的信息或例子。我还遇到了一个问题,就是原始函数是使用旧式(osv)编写的,而我正在使用新式(models)。

根据我在互联网上收集的信息,我写了类似于以下内容的代码(但它不起作用)。

class product_product_inherit(models.Model): 
    _inherit = 'product.product'
    
    #api.v7 because of old style? Also tried .multi and .model...  
    @api.v7
    def _product_available(self, cr, uid, ids, field_names=None, arg=False, context=None):
        #example of modified values. To be made variable after this is working.
        res = {8: {'qty_available': 200.222, 'outgoing_qty': 1778.5, 'virtual_available': 30205.263671875, 'incoming_qty': 0.0}}
        result = super(C, self)._product_available(res)    
        return result

有人知道正确的方法来修改原始函数 _product_available返回的字典吗?

2个回答

1
我认为你可以尝试这个。
class ProductProductInherit(models.Model): 
    _inherit = 'product.product'

    @api.multi
    def _product_available(self, field_names=None, arg=False):
        #example of modified values. To be made variable after this is working.
        res = {8: {'qty_available': 200.222, 'outgoing_qty': 1778.5, 'virtual_available': 30205.263671875, 'incoming_qty': 0.0}}
        result = super(ProductProductInherit, self)._product_available(res)    
        return result

已经解决了。无论如何还是谢谢!我会在我的原始帖子中发布我是如何做到的。 - RobbeM

0
提问者的解决方案(已从问题中删除答案数据): 我是如何让它工作的:
class product_product_inherit(models.Model): 
    _inherit = 'product.product'

    def _product_available(self, cr, uid, ids, field_names=None, arg=False, context=None):
        for product in self.browse(cr, uid, ids, context=context):
            id = product.id
            res = {id: {'qty_available': 200.222, 'outgoing_qty': 1778.5, 'virtual_available': 30205.263671875, 'incoming_qty': 0.0}}
        return res

刚刚定义了与原始模型完全相同的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接