Odoo: How to override original function

雨燕双飞 提交于 2019-12-21 21:27:38

问题


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.

This function returns a dictionary called 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.

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.

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

Does anyone know the correct way to modify the returned dictionary of the original function _product_available?

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.


回答1:


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


来源:https://stackoverflow.com/questions/32859723/odoo-how-to-override-original-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!