Skip to Content
Menu
This question has been flagged
8 Replies
17086 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 ?


Avatar
Discard
Best Answer

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))
Avatar
Discard
Author

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

Author

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

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

Author

O yeah, thank you Axel :)

Author

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}) ??

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})

Author

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})
Author

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})
Best Answer

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})
Avatar
Discard
Author

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 '===================================================================================='
Author

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 ?

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

Related Posts Replies Views Activity
5
Dec 15
12265
0
Oct 24
134
1
Sep 23
1073
2
Apr 23
85060
1
Sep 19
3927