Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
7248 Lượt xem

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>

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất
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

Ảnh đại diện
Huỷ bỏ
Tác giả

Vielen danke Gerhard

Tác giả

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 ?

Tác giả
	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.

Tác giả

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 5 20
2966
6
thg 3 24
37406
1
thg 3 17
10763
0
thg 4 24
1931
4
thg 11 23
6030