简体   繁体   中英

Odoo: How to override original function

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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