Odoo Help

Welcome!

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

0

Record create/write in Odoo8.0 new API

By
Dr Obx
on 2/4/16, 5:41 PM 1,505 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

--Axel Mendoza--
10308
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

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

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3918
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 2/5/16, 5:51 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 2/4/16, 5:41 PM
Seen: 1505 times
Last updated: 2/18/16, 3:02 AM