Odoo Help

Welcome!

This community 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.

0

Getting Data From First Item Of o2m List

By
Alex Gallien
on 7/2/15, 3:57 PM 731 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

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 7/3/15, 1:29 AM

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'

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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