简体   繁体   English

Odoo:如何覆盖原始功能

[英]Odoo: How to override original function

In Odoo, the quantities of a product are calculated each time the products form is opened.在 Odoo 中,每次打开产品表单时都会计算产品的数量。 This happens in model product.product ==> function _product_available .这发生在模型product.product ==> function _product_available

This function returns a dictionary called res.此函数返回一个名为 res 的字典。

Example:例子:

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

Now I want to modify those values.现在我想修改这些值。 I've managed to do this by coding it directly in the original function _product_available .我已经通过直接在原始function _product_available编码来做到这function _product_available

Since this is not the correct way to do this, I want to do this in an inheritted model.由于这不是执行此操作的正确方法,因此我想在继承模型中执行此操作。 I think I need to override the function ?我想我需要覆盖该function Or overwrite?还是覆盖? Not sure what it's called.不确定它叫什么。

Everything I read about doing this is quite vague to me.我读到的关于这样做的所有内容对我来说都很模糊。 I can't find much good information or examples.我找不到太多好的信息或例子。 I'm also struggling with the fact that the original function is written in old style ( osv ) while I'm using new style ( models ).我也在努力解决以下事实:原始函数是用旧样式 ( osv ) 编写的,而我正在使用新样式 ( models )。

From pieces of information I collected on the internet I wrote something like this (which doesn't work).根据我在互联网上收集的信息,我写了这样的东西(不起作用)。

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

Does anyone know the correct way to modify the returned dictionary of the original function _product_available ?有谁知道修改原始function _product_available返回字典的正确方法吗?

I think you can try this.我想你可以试试这个。

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

The asker's solution (answer data removed from the question):提问者的解决方案(从问题中删除了答案数据):

How I got it working:我是如何让它工作的:

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

Just defined exactly the same method as in the original model.刚刚定义了与原始模型完全相同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM