Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
7247 Näkymät

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
Hylkää
Paras vastaus
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
Hylkää
Tekijä

Vielen danke Gerhard

Tekijä

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 ?

Tekijä
	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.

Tekijä

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

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
0
toukok. 20
2966
6
maalisk. 24
37406
1
maalisk. 17
10763
0
huhtik. 24
1931
4
marrask. 23
6030