Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

v15: I want a Python script that can generate a CSV file of Odoo records. Can someone give me a sample?

Subscribe

Get notified when there's activity on this post

This question has been flagged
pythoncsvscriptv15
2 Replies
6017 Views
Avatar
Community Question

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?



0
Avatar
Discard
Avatar
Ray Carnes (ray)
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.

1
Avatar
Discard
Avatar
Pete Charalampopoulos
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

0
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
My Odoo 15 Instance stops a few minutes after startup
python v15
Avatar
0
Nov 23
2181
Create order_line from code Solved
python v15
Avatar
Avatar
1
Nov 22
5040
Update field value using studio with python code
python studio v15
Avatar
Avatar
Avatar
2
Mar 24
6030
How to correctly create subfolders of models
python importing v15
Avatar
Avatar
1
Mar 23
3608
Odoo custom module update - base action rule error
python odoo10.0 v15
Avatar
0
Oct 22
3724
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now