Odoo:如何覆盖原始功能

问题描述 投票:3回答:1

在Odoo中,每次打开产品表单时都会计算产品的数量。这在模型product.product ==>函数_product_available中发生。

此函数返回一个称为res的字典。

示例:

res = {8: {'qty_available': 5000.0, 'outgoing_qty': 1778.5, 'virtual_available': 3221.5, 'incoming_qty': 0.0}}

现在我要修改这些值。我设法通过直接在原始函数_product_available中对其进行编码来做到这一点。

由于这不是正确的方法,因此我想在继承的模型中进行。我想我需要重写功能吗?还是覆盖?不知道它叫什么。

我读到的关于此操作的所有内容对我来说都是很模糊的。我找不到很多好的信息或示例。我还在为原始函数以旧样式(osv)编写而我在使用新样式(模型)的情况下感到挣扎。

从我在互联网上收集到的信息中,我写了这样的东西(不起作用)。

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的返回字典的正确方法吗?

我的工作方式:

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

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

python overriding odoo overwrite
1个回答
0
投票

我认为您可以尝试这个。

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
© www.soinside.com 2019 - 2024. All rights reserved.