This question has been flagged
2 Replies
3429 Views

If I wanted to get the name of the Company I have sold a subscription to as well as the Country and Email address of the Customer - three simple columns - how would I do this?



Avatar
Discard
Best Answer

Documentation on using the Odoo External API can be found at https://www.odoo.com/documentation/15.0/developer/misc/api/odoo.html

We recommend setting up API KEYS.

The first part of your script needs to authenticate with the Odoo Server:

import xmlrpc.client

db = "database_name"
url = "https://mydatabaseurl.odoo.com"

username = "YOUR-USER-LOGIN"
password = "YOUR-API-KEY"

common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))

uid = common.authenticate(db, username, password, {})


The second part needs to search for and read the data you want extracted and will reference the object endpoint (using the uid and password defined in the prior snippet):

object = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

subscription_ids = object.execute_kw(db, uid, password,
'sale.subscription', 'search_read',
[[[]]],
{'fields': ['partner_id','country_id']})

for subscription in subscription_ids:
partner_id = int(subscription['partner_id'][0])
country = subscription['country_id'][1]

partner_ids = object.execute_kw(db, uid, password,
'res.partner', 'search_read',
[[['id','=',partner_id]]],
{'fields': ['parent_id','email'], 'limit': 1})

for partner in partner_ids:
company = partner['parent_id'] and partner['parent_id'][1] or subscription['partner_id'][1]
email = partner['email']

print ('%s, %s, %s' % (country,company,email))

Then you can run this to generate your output

python3 get_odoo_data.py > /tmp/odoo_data.csv


Note that this is a sample script with no error checking written this way to explain the API, I am not promoting a best practice here. There will only ever be one partner found during the search being stored in partner_ids for example, so you can also use list and dictionary parsing if you know how to do that. You can also see code that finds EITHER the Display Name of the Parent of the Customer (aka: the Company of the Contact - if one exists) OR the Display Name of the Customer.

Avatar
Discard
Best Answer

How would the second part will be if there is only a simple table. lets say i have field_1, field_2, field_3

and so on

Avatar
Discard