Odoo Help

0

Record create/write in Odoo8.0 new API

By
Dr Obx
on 2/4/16, 5:41 PM 3,576 views

How to do it ?:

def check_code(self):

print '== Check postcode =================================================================='

code_check = self.env['post.coder'].search([('postcode','=',self.buyer_postcode)])# reading code, if code exist

it show me the code correctly, If code doesn't exist False is printed on screen. So now i would like to update record and add street name where the code is or if code not exist in database  add new record.

if code_check is False:

# this condition apparently doesn't work

    print 'Post code:', code_check.postcode

else:

    #execute(""" UPDATE post_coder SET street = %s WHERE postcode = %s""" %(self.buyer_address_1,self.buyer_postcode))

print ' Post code updated'

How using non python query add or update record in table ?


1
Axel Mendoza
On 2/4/16, 5:55 PM

using the api

self.env['post.coder'].write(code_check, {'street': self.buyer_address_1})

or use the cursor like:

self.env.cr.execute(""" UPDATE post_coder SET street = %s WHERE postcode = %s""" %(self.buyer_address_1,self.buyer_postcode))

Thank you Axel But why if statement won't work ?

Dr Obx
on 2/4/16, 6:05 PM

How can I do it in more Odoo style ? ... self.env['post.coder'].write([('postcode','=',self.buyer_postcode)]) ???

Dr Obx
on 2/4/16, 6:06 PM

Check the answer again to see how to do it using the api

Axel Mendoza
on 2/4/16, 6:12 PM

O yeah, thank you Axel :)

Dr Obx
on 2/4/16, 6:19 PM

What if I would like to update/write more than one element ? self.env['post.coder'].write(code_check, {'street1': self.buyer_address_1,'street2': self.buyer_address_2}) ??

Dr Obx
on 2/4/16, 6:23 PM

exactly as you put it, and if you wanna update more that one records with the same data you could do it like:
self.env['post.coder'].write([code_check, code_check1, code_check2], {'street1': self.buyer_address_1,'street2': self.buyer_address_2})

Axel Mendoza
on 2/4/16, 6:56 PM

Hey Axel, just to clarify, the UPDATE API command should look like this:

self.env['res.partner'].write({'name': record.buyer_full_name.title(), 'city': record.buyer_city.title(), 'street': record.buyer_address_1.title(), 'street2': record.buyer_address_2.title(), 'zip': postcode.upper(),'email': record.buyer_email.lower(), 'phone': record.buyer_phone_number})
the INSERT API command should look like this:
self.env['res.partner'].create({'name': record.buyer_full_name.title(), 'city': record.buyer_city.title(), 'street': record.buyer_address_1.title(), 'street2': record.buyer_address_2.title(), 'zip': postcode.upper(),'email': record.buyer_email.lower(), 'phone': record.buyer_phone_number})
Am I right ? What is this then:
self.env['post.coder'].write(code_check, {'street': self.buyer_address_1})
Dr Obx
on 2/17/16, 7:33 AM

As I understand

self.env[table.name].operation(what_is_this{'field': value})
and what if I will try to write something without 'what_is_this'? look below (example):
self.env['res.partner'].write({'name': record.buyer_full_name.title(), 'city': record.buyer_city.title(), 'street': record.buyer_address_1.title(), 'street2': record.buyer_address_2.title(), 'zip': postcode.upper(),'email': record.buyer_email.lower(), 'phone': record.buyer_phone_number})
Dr Obx
on 2/17/16, 7:37 AM
0

The result of code_check is a list of recordset.
You can use the python methods available in model using the recordset itself.

code_check.write({'street': self.buyer_address_1})

Thank you Atchuthan, and it works, but if I would like to update record with more than one element i mean: self.env.cr.execute(""" UPDATE post_coder SET street = '%s' number = '%s' and something WHERE postcode = '%s'""" %(self.buyer_address_1,self.buyer_house_number,self.buyer_postcode)) I'll ask again, what if I would like to update/write more than one element ? self.env['post.coder'].write(code_check, {'number': self.buyer_address_1,'street': self.buyer_address_2,'postcode': self.buyer_postcode}) ?? I tried it that way but I'm getting an error or is doing nothing. example:

	def check_code(self):
		print '== Checking postcode ==============================================================='
		print '   Postcode check initiated ...'
		code_check = self.env['post.coder'].search([('postcode','=',self.buyer_postcode)])
		if code_check == False:
			print 'No postcode in database, writing ....'
			self.env['post.coder'].write(code_check, {'number': self.buyer_address_1,'street': self.buyer_address_2,'postcode': self.buyer_postcode})
			print 'Post code added'
		else:
			self.env.cr.execute(""" UPDATE post_coder SET street = '%s' WHERE postcode = '%s'""" %(self.buyer_address_1,self.buyer_postcode))
			print '   Post code updated:',code_check.postcode
			print '===================================================================================='
Dr Obx
on 2/5/16, 6:16 AM

So if I get as a result False (no postcode in database) i would like to add it and also if postcode is already in database, updating only street and number (this part works :)) What i've done wrong ?

Dr Obx
on 2/5/16, 6:18 AM

code_check.write({'number': self.buyer_address_1,'street': self.buyer_address_2,'postcode': self.buyer_postcode}) You can add n number of fields to update in write method

Atchuthan - Technical Consultant, Sodexis Inc
on 2/8/16, 12:36 AM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 2/4/16, 5:41 PM
Seen: 3576 times
Last updated: 12/16/16, 6:03 AM