I'm working with the xmlrpc api using Python 3.
I'm having some trouble understanding the syntax for creating or updating a one2many or many2many type.
I've figured out the basic create and read commands for generic data types, but I can't seem to wrap my head around the documentation for the others, nor can I find much external help on the matter.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project management
- MRP
This question has been flagged
Here is a code snippet that will create a new Partner, a new Contact, then update the Contact.
import xmlrpc.client
#connection details
server_url = 'http://localhost:8069'
db = 'database_name'
login = 'admin'
password = 'admin'
#endpoints
common_ep = xmlrpc.client.ServerProxy(server_url+'/xmlrpc/2/common')
object_ep = xmlrpc.client.ServerProxy(server_url+'/xmlrpc/2/object')
#login
uid = common_ep.authenticate(db, login, password,{})
#define new Partner
new_partner = {
'name': 'Joe Smith',
'ref': '12345',
}
#create New Partner
new_partner_id = object_ep.execute_kw(db, uid, password, 'res.partner', 'create', [new_partner])
#define New Contact
new_contact = {
'name': 'Jane Smith',
'ref': '54321',
'parent_id': new_partner_id,
}
#create New Contact, under New Partner
new_contact_id = object_ep.execute_kw(db, uid, password, 'res.partner', 'create', [new_contact])
#define update for New Contact
updated_information = {
'phone': '555 123 1234',
}
#find New Contact
existing_contact_id = object_ep.execute_kw(db, uid, password, 'res.partner', 'search', [[['ref','=','54321']]])
object_ep.execute_kw(db, uid, password, 'res.partner', 'write', [existing_contact_id, updated_information])
There's documentation on the "write" method for the special syntax for these o2m and m2m fields: https://www.odoo.com/documentation/12.0/reference/orm.html#model-reference
However, I would suggest using a library to wrap the XML-RPC or JSON-RPC calls with a simpler syntax.
For example you can use either \Odooly or OdooRPC libs (available on PyPI and Github).
Sample code with odooly (doing the same stuff as the comment above):
import odooly
client = odooly.Client('http://localhost:8069/jsonrpc', 'database_name', 'admin', 'admin')
env = client.env
# define new Partner
partner_data = {
'name': 'Joe Smith',
'ref': '12345',
}
# create New Partner
new_partner = env['res.partner'].create(partner_data)
# define New Contact
contact_data = {
'name': 'Jane Smith',
'ref': '54321',
'parent_id': new_partner.id,
}
new_contact = env['res.partner'].create(contact_data)
updated_information = {
'phone': '555 123 1234',
}
#find New Contact
existing_contact = env['res.partner'].search(['ref = 54321'])
existing_contact.write(updated_information)
----
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign upRelated Posts | Replies | Views | Activity | |
---|---|---|---|---|
|
1
Jul 18
|
6064 | ||
|
3
Jan 23
|
4833 | ||
|
1
Mar 22
|
3580 | ||
|
1
Feb 22
|
3828 | ||
|
1
Jul 20
|
7814 |