Odoo:如何覆盖原始功能 [英] Odoo: How to override original function

查看:110
本文介绍了Odoo:如何覆盖原始功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Odoo, the quantities of a product are calculated each time the products form is opened. This happens in model product.product ==> function _product_available.

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

This function returns a dictionary called res.

示例:

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

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

Now I want to modify those values. I've managed to do this by coding it directly in the original 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? Or overwrite? Not sure what it's called.

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

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).

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

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

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

Does anyone know the correct way to modify the returned dictionary of the original function _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

定义的方法与原始模型中的方法完全相同.

Just defined exactly the same method as in the original model.

推荐答案

我认为您可以尝试一下.

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

这篇关于Odoo:如何覆盖原始功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆