Skip to Content
Menu
This question has been flagged
1 Reply
39257 Views

[code]

....

   'current_stock': fields.function(_get_current_stock, type='char', string='Currently in stock'),
....


 #getting quants from stock.quant to put into field 'current_stock'

def _get_current_stock(self, cr, uid, ids, field_name, arg, context=None):

res = []

in_stock = 0

tot_sold = 0

product_id = 0

for obj in self.browse(cr, uid, ids, context=context):

#first query read data about product sold
cr.execute(""" select sum(qty) from stock_quant where product_id = %s and location_id = 7;""" %(obj.product_id.id))
tot_sold = cr.fetchall()

#second query read data adout products left in stock
cr.execute(""" select sum(qty) from stock_quant where product_id = %s and location_id != 7;""" %(obj.product_id.id))
in_stock = cr.fetchall()

res = in_stock[0]    <= once i add this, everything stops and getting error, of course if I remove it got no error but also current_stock stays empty because res = ''

# I can print value of tot_sold and in_stock
print '|| Total sell: ', tot_sold[0], ' Left in stock: ',in_stock[0]

return res

 Getting this error and don't know how to extract data from res and populate with related field 'current_stock'

new_values = dict(values)

TypeError: cannot convert dictionary update sequence element #0 to a sequence

Avatar
Discard

Please use significant titles, this forum rule is also valid for newbies :-)

Best Answer

Hello Dr Obx,

I guess the problem is that you are not passing the value to associated record.

Try following code:

res[obj.id] = in_stock[0]

Reference: Function Field.

Hope this will help you.

Avatar
Discard
Author

Thank you Serpent :) Unfortunately still getting error, but this time: File "/usr/lib/python2.7/dist-packages/openerp/addons/iProdLoc/iprodloc.py", line 229, in _get_current_stock res[obj.id] = in_stock[0] IndexError: list assignment index out of range

You must check whether there are any records and then set in_stock[0]! Set like res[obj.id] = len(in_stock) !=0 and in_stock[0] or False.

Author

So far I managed to partially solve the problem, now I don't get those errors anymore but still cant put value into my field ;)

Related Posts Replies Views Activity
0
Dec 15
2471
5
Dec 23
73340
0
Jul 16
2776
3
Jan 16
8171
1
Feb 16
3291