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

Query vs API - how to ?

By
Dr Obx
on 2/8/16, 7:01 AM 491 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>

1
Gerhard Baumgartner
On 2/8/16, 7:11 AM
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

Vielen danke Gerhard

Dr Obx
on 2/8/16, 7:12 AM

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 ?

Dr Obx
on 2/8/16, 7:14 AM
	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
Dr Obx
on 2/8/16, 7:16 AM

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.

Gerhard Baumgartner
on 2/8/16, 7:32 AM

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

Gerhard Baumgartner
on 2/8/16, 7:34 AM

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

Dr Obx
on 2/8/16, 8:38 AM

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: 2/8/16, 7:01 AM
Seen: 491 times
Last updated: 2/8/16, 9:17 AM