This question has been flagged
3 Replies
5935 Views

I am trying to store the products function fields in DB. Using "store=True" attribute to save the value in DB, that has updated my table structure but the function field value hasn't updated in table. Look out my code below:

class product_product(osv.osv):
    _inherit = 'product.product'

def _get_product_available_func(states, what):
        def _product_available(self, cr, uid, ids, name, arg, context=None):           
            return {}.fromkeys(ids, 0.0)
        return _product_available

    _product_qty_available = _get_product_available_func(('done',), ('in', 'out'))
    _product_outgoing_qty = _get_product_available_func(('confirmed','waiting','assigned'), ('out',))
    _product_incoming_qty = _get_product_available_func(('confirmed','waiting','assigned'), ('in',))

_columns = {
        'qty_available': fields.function(_product_qty_available, type='float', store=True, string='Quantity On Hand'),
        'incoming_qty': fields.function(_product_incoming_qty, type='float', store=True, string='Incoming'),
        'outgoing_qty': fields.function(_product_outgoing_qty, type='float', store=True, string='Outgoing'),
}
product_product()

Please help me to store the value in DB whenever i am updating.

Avatar
Discard
Best Answer

You need to edit the record one by one to trigger the function field storage.  Or you can create a module to do just that.  One advise, I think the qty fields in product.product have been deliberately made to be not stored as it is also used to count quantity in specific location/warehouse/stock production lot/etc.  So, if you store it, it may be very heavy in I/O and may lead to inconsistent calculation in other functions (as now the qty field will be read off the table first).

What is your reason to implementing the store=True?

Avatar
Discard
Best Answer

Hello,

In V8, Odoo implemented a _funct_seach paramter for those values, i don't know how efficient it is (as the method browse all the product catalog each time) but at least, its possible to filter on those qties.

I know this topic is a bit old, but i just tried to make a similar solution, and i wanted to share what i found browsing the Odoo code :

  • First, the code you have to override must be the one in "stock" module, this code in "product" module you are trying to override is just here to supply a default value for the stock qties (0.0, look at the code ^^)
  • Then, a simple store=True is useless, because store=True means the field value will only be updated when the product is changed. The problem is that the stock qties are changed by the tables stock.move, stock.location (perharps more, i have not browse all the modules) and it seems they are influenced by the company (-ies) of the current user.
  • Finally, i think a simple store parameter would not be efficient, or at least it could be but only on "simple" company instance

To conclude, if we are on a single company structure:

=> We can add a store parameter, listenning to "stock.move" and "stock.location" tables

If we are on a multi-company structure:

=> i don't know how to do it for now, but a way to explore could be to add a new one2many field on product.product, related to a new table, to store each stock qties for each company. It could be this new table that has a store parameter (as described in single company structure). Finally, we add a _fnct_search (on product.product) parameter that will only use this new one2many relation.

Hope it will be useful for someone, i'm aware of comments ;)

Regards

Avatar
Discard

There is one more catch Bruno. Once you set the store=True, ORM will read off the column's content first before calculating. This will pose a problem because the qty fields are meant to be dynamic and calculated depending on the context supplied (it may be for a specific location(s), warehouse(s), etc.).

Yes, you're right. Perharps the better approach is to create custom field(s) according to what we want to do with it (only displays the current qties, all locations merged), and store as we want. Overriding this field (which seems to be used in many modules) can be too dangerous and may broke some codes. For Odoo servers lower than V8, it may be a good solution to (re-) implement a _funct_search as the V8 did (because we keep all company, location, .. context values) . I did that for one of my client (in a custom module), it works but hopefully, they don't have 400,000 records in product.product ^^

Best Answer

dict.fromkeys(seq[, value])) Parameters seq -- This is the list of values which would be used for dictionary keys preparation.

value -- This is optional, if provided then value would be set to this value ///////////// return {}.fromkeys(ids, 0.0) ? you can change return {}.fromkeys(ids)

Avatar
Discard

In this case, it is better to use default value 0.0 as qty will always be considered as a float. If you don't specify value, you'll end up with None as the value, and this may cause problems if you include it in calculations. However, I believe your answer does not relate to the question.