تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
7249 أدوات العرض

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>

الصورة الرمزية
إهمال
أفضل إجابة
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

الكاتب

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 ?

الكاتب
	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.

الكاتب

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

المنشورات ذات الصلة الردود أدوات العرض النشاط
0
مايو 20
2966
6
مارس 24
37407
1
مارس 17
10767
0
أبريل 24
1931
4
نوفمبر 23
6030