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

Guys, I'm a newbie, still learning and quiet often I have a dilemma how should I do this or that :)

So if would you like to explain and enlighten it a bit for me please.

For example:

<pre>

cr.execute("""SELECT sum(qty) from stock_quant where location_id = %s""" % obj.id)

tot_qty = cr.fetchall()

and ...

how to do the same using old API ?

self.pool.get(stock.quant)..search(cr, uid, [('usage', '=', 'production')], context=context)

like that ?

and how should it look like in new API ?

</pre>

Avatar
Discard
Best Answer
self.env['stock.quant'].search([('usage', '=', 'production')])

or more generic:

self.env['model.name'].search([('variable', 'operator', 'value')])

at search you can also limit the recordsets:

self.env['model.name'].search([('variable', 'operator', 'value')], limit=1)

accessmembers directly:

self.env['model.name'].search([('variable', 'operator', 'value')])[0].name

and much more.

is new api. If you're using odoo8 or 9 I'd stick with the new API style.


On a side note: If you want to add or modify records and you've got computed fields, stay away from using queries as computed fields will not be calculated using raw queries.


Further reading: http://odoo-new-api-guide-line.readthedocs.org/en/latest/environment.html


edit: improved answer

Avatar
Discard
Author

Vielen danke Gerhard

Author

What if my module is already in old API style ? def name_get(self, cr, uid, ids, context=None): res = super(stock_location, self).name_get(cr, uid, ids, context=context) res1=[] tot_qty = 0 product_id = 0 if context.has_key('prod_id'): product_id = context.get('prod_id', 0) for obj in self.browse(cr, uid, ids): cr.execute("""SELECT sum(qty) from stock_quant where location_id = %s""" % obj.id) tot_qty = cr.fetchall() if product_id: cr.execute("""SELECT sum(qty) from stock_quant where location_id = %s and product_id = %s""" % (obj.id, product_id)) tot_qty = cr.fetchall() if str(tot_qty[0][0]) == 'None': obj.write({'cntnt': str(tot_qty[0][0])}) if str(tot_qty[0][0]) != 'None': obj.write({'cntnt': str(tot_qty[0][0])}) res1.append((obj.id, obj.name + ' ('+str(tot_qty[0][0])+' in Stock)')) return res1 This is an example .... How would you do this in new API style ?

Author
	def name_get(self, cr, uid, ids, context=None):
		res = super(stock_location, self).name_get(cr, uid, ids, context=context)
		res1=[]
		tot_qty = 0
		product_id = 0
		if context.has_key('prod_id'):
			product_id = context.get('prod_id', 0)
		for obj in self.browse(cr, uid, ids):
			cr.execute("""SELECT sum(qty) from stock_quant where location_id = %s""" % obj.id)
			tot_qty = cr.fetchall()
			if product_id:
				cr.execute("""SELECT sum(qty) from stock_quant where location_id = %s and product_id = %s""" % (obj.id, product_id))
				tot_qty = cr.fetchall()
			if str(tot_qty[0][0]) == 'None':
				obj.write({'cntnt': str(tot_qty[0][0])})
			if str(tot_qty[0][0]) != 'None':
				obj.write({'cntnt': str(tot_qty[0][0])})
			res1.append((obj.id, obj.name + ' ('+str(tot_qty[0][0])+' in Stock)'))
		return res1

There's not much to change using new API here: def name_get(self): res = super(stock_location, self).name_get() res1 = [] tot_qty = 0 product_id = 0 if 'prod_id' in self._context: product_id = self._context.get('prod_id', 0) for obj in self.browse(self._ids): self._cr.execute("""SELECT sum(qty) from stock_quant where location_id = %s""" % obj.id) tot_qty = self._cr.fetchall() if product_id: self._cr.execute("""SELECT sum(qty) from stock_quant where location_id = %s and product_id = %s""" % ( obj.id, product_id)) tot_qty = self._cr.fetchall() if str(tot_qty[0][0]) == 'None': obj.write({'cntnt': str(tot_qty[0][0])}) if str(tot_qty[0][0]) != 'None': obj.write({'cntnt': str(tot_qty[0][0])}) res1.append((obj.id, obj.name + ' (' + str(tot_qty[0][0]) + ' in Stock)')) return res1 The main difference is that things like cr or context are now in self. You can stick with the queries because SELECT is often faster and won't make problems.

Odoo either needs formatting options on comments, or i can't find them. Sorry for bad formatting anyways.

Author

Just add &lt pre &gt and &lt/pre&gt tags

Related Posts Replies Views Activity
0
May 20
1998
6
Mar 24
35768
1
Mar 17
9781
0
Apr 24
396
4
Nov 23
4242