Odoo Help

0

Getting Data From First Item Of o2m List

By
Alex Gallien
on 7/2/15, 3:57 PM 1,132 views

I am trying to get my supplier prices listed in a custom view (modified version of product_supplier_info module) but can't figure out how to pull the prices from the 'pricelist_ids' field correctly. We only have one price per supplier (no bulk price breaks), so I only need the data from the first line. Anyone know how I could pull that? Are o2m stored as arrays?

EDIT: In case anyone else is trying to do this, here is what I ended up coming up with:

from osv import osv, fields
from openerp import tools
from tools.translate import _
import openerp.addons.decimal_precision as dp
class product_supplierinfo(osv.osv):
_inherit = 'product.supplierinfo'
def _current_price(self, cr, uid, ids, field_names=None, arg=False, context=None):
if context is None:
context = {}
res = {}
for supp_nfo in self.browse(cr, uid, ids, context=context):
res[supp_nfo.id] = 0.0
if supp_nfo.pricelist_ids:
res[supp_nfo.id] = supp_nfo.pricelist_ids[0].price
return res
_columns={
'x_cur_price' : fields.function(_current_price,type='float',string="Price"),
}
product_supplierinfo()

Worked like a charm, thanks for the help everyone.

0
khuong
On 8/12/15, 9:30 PM

Hi Alex, 


Server Traceback (most recent call last):

File "/usr/lib/pymodules/python2.7/openerp/addons/web/session.py", line 89, in send

return openerp.netsvc.dispatch_rpc(service_name, method, args)

File "/usr/lib/pymodules/python2.7/openerp/netsvc.py", line 296, in dispatch_rpc

result = ExportService.getService(service_name).dispatch(method, params)

File "/usr/lib/pymodules/python2.7/openerp/service/web_services.py", line 626, in dispatch

res = fn(db, uid, *params)

File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 190, in execute_kw

return self.execute(db, uid, obj, method, *args, **kw or {})

File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 132, in wrapper

return f(self, dbname, *args, **kwargs)

File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 199, in execute

res = self.execute_cr(cr, uid, obj, method, *args, **kw)

File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 187, in execute_cr

return getattr(object, method)(cr, uid, *args, **kw)

File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 3679, in read

result = self._read_flat(cr, user, select, fields, context, load)

File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 3788, in _read_flat

res2 = self._columns[val[0]].get(cr, self, ids, val, user, context=context, values=res)

File "/usr/lib/pymodules/python2.7/openerp/osv/fields.py", line 1148, in get

for field, value in result[id].iteritems():

AttributeError: 'float' object has no attribute 'iteritems'

</code>

It's error because function field must to return dict with keys are every id in ids.

You can replace:

res = {} 

to

res = dict.fromkeys(ids, 0.0) 


Thanks! You helped me get on the right track. For some reason 'res = dict.fromkeys(ids, 0.0)' wasn't working for me. I think it was putting the list 'ids' in each of the keys of that dictionary and giving errors. Posting the code I ended up going with in the OP.

Alex Gallien
on 8/12/15, 10:41 PM
0

Hello Alex,

It's very easy. Follow below steps,

1. Create a new float field ( kind of field.function ) in product.supplierinfo model.

2. In the method of field.function ( as per step 1) find the pricelist_ids field. If there is value inside then return the first value.

Note : Don't forget to add attribute (store=true) inside that field.function. The reason is, if you want to search by Price in the list view of product.supplierinfo model, then you will able to do if "store=true" is written inside that functional field.

Code is very easy, if you don't know then let me know I will write the code here. ( I thought you just need a logic )

Hope this helps,

Hi, thanks for this response. Only now getting to this request after some time. I would really appreciate it if you could give me the code for this, I don't think I exactly understand the syntax and it would be very helpful. Thanks!!

Alex Gallien
on 8/12/15, 8:13 PM

I posted the code I tried that ultimately failed. I feel like it is close, but I am not sure what needs to be tweaked. Thanks again for your help.

Alex Gallien
on 8/12/15, 8:46 PM
0
Alex Gallien
On 8/12/15, 8:45 PM

So I haven't quite figured out all these ORM methods yet, but this is what I tried (amongst other attempts):

from osv import osv, fields
from openerp import tools
from tools.translate import _
import openerp.addons.decimal_precision as dp
class product_supplierinfo(osv.osv):
_inherit = 'product.supplierinfo'
def _current_price(self, cr, uid, ids, field_names=None, arg=False, context=None):
res = {}
for supp_nfo in self.browse(cr, uid, ids, context=context):
if supp_nfo.pricelist_ids:
res[supp_nfo.id] = supp_nfo.pricelist_ids[0].price
return res
_columns={
'x_cur_price' : fields.function(_current_price,multi='x_cur_price',type='float',string="Price"),
}
product_supplierinfo()

This is the error I get:

Server Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/openerp/addons/web/session.py", line 89, in send
return openerp.netsvc.dispatch_rpc(service_name, method, args)
File "/usr/lib/pymodules/python2.7/openerp/netsvc.py", line 296, in dispatch_rpc
result = ExportService.getService(service_name).dispatch(method, params)
File "/usr/lib/pymodules/python2.7/openerp/service/web_services.py", line 626, in dispatch
res = fn(db, uid, *params)
File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 190, in execute_kw
return self.execute(db, uid, obj, method, *args, **kw or {})
File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 132, in wrapper
return f(self, dbname, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 199, in execute
res = self.execute_cr(cr, uid, obj, method, *args, **kw)
File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 187, in execute_cr
return getattr(object, method)(cr, uid, *args, **kw)
File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 3679, in read
result = self._read_flat(cr, user, select, fields, context, load)
File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 3788, in _read_flat
res2 = self._columns[val[0]].get(cr, self, ids, val, user, context=context, values=res)
File "/usr/lib/pymodules/python2.7/openerp/osv/fields.py", line 1148, in get
for field, value in result[id].iteritems():
AttributeError: 'float' object has no attribute 'iteritems'

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 7/2/15, 3:57 PM
Seen: 1132 times
Last updated: 8/12/15, 10:42 PM