Skip to main content

BetterOdooApiWrapper

A minimal Python ORM wrapper for the Odoo API.

Overview

BetterOdooApiWrapper is a lightweight, Pythonic wrapper around the Odoo XML-RPC API, providing an ORM-like interface to interact with Odoo models and records. It simplifies the process of querying, filtering, and manipulating data in Odoo from Python applications.

Features

  • Pythonic ORM Interface: Interact with Odoo models using familiar Python syntax.
  • Dynamic Model Access: Access any Odoo model dynamically without pre-defining classes.
  • Complex Querying: Support for filtering, ordering, and selecting specific fields.
  • Relational Fields Handling: Seamlessly work with many2one, one2many, and many2many fields.
  • Context Management: Easily set and update the Odoo context for your queries.
  • Export Functionality: Export data, including nested relational fields, efficiently.

Getting Started

Instalation

Install the module using PyPI

pip install BetterOdooApiWrapper

Connecting to Odoo

from BetterOdooApiWrapper import Client

# Initialize the Odoo client
odoo = Client(
    url="https://your-odoo-instance.com",
    db="your-database-name",
    username="your-username",
    password="your-password"
)

Setting Context

Optionally, you can set the context for your operations:

# Set the context for subsequent queries
odoo.set_context(lang='en_US', tz='UTC')

Accessing Models

# Access the 'res.partner' model
partners = odoo['res.partner']

Querying Data

Selecting Fields

# Select specific fields
partners = partners.select(lambda p: (p.name, p.email))

Filtering Data

# Filter partners where name contains 'John' and email is not null
partners = partners.filter(lambda p: ('John' in p.name, p.email != False))

Ordering Results

# Order by name ascending
partners = partners.order_by(lambda p: p.name)

# Order by name descending
partners = partners.order_by_descending(lambda p: p.name)

Limiting Results

# Limit to first 10 records
partners = partners.take(10)

Fetching the Data

# Execute the query and get the results
results = partners.get()

Fetching a Single Record

# Get the first matching record
partner = partners.first()

Working with Relational Fields

# Select fields from related models
partners = partners.select(lambda p: (
    p.name,
    p.company_id.name,
    p.company_id.country_id.name
))
results = partners.get()

Exporting Data

Use the export method to fetch data using Odoo's export_data method, which is efficient for large datasets.

# Export data including nested relational fields
data = partners.export()

Filtering by External IDs

# Filter records by their external IDs
partners = partners.external_ids(['module.partner_1', 'module.partner_2'])
results = partners.get()

Full Example

from BetterOdooApiWrapper import Client

# Initialize the client
odoo = Client(
    url="https://your-odoo-instance.com",
    db="your-database-name",
    username="your-username",
    password="your-password"
)

# Set context if needed
odoo.set_context(lang='en_US', tz='UTC')

# Build the query
partners = (
    odoo['res.partner']
    .select(lambda p: (
        p.name,
        p.email,
        p.company_id.name,
        p.company_id.country_id.name
    ))
    .filter(lambda p: (
        p.is_company == True,
        p.customer_rank > 0
    ))
    .order_by(lambda p: p.name)
    .take(50)
)

# Execute the query
results = partners.get()

# Close the client connection
odoo.close()

# Process the results
for partner in results:
    print(f"Name: {partner['name']}, Email: {partner['email']}")
    print(f"Company: {partner['company_id']['name']}")
    print(f"Country: {partner['company_id']['country_id']['name']}")

Creating Records

You can create single or bulk records in odoo by supplying a list of key: value dictionaries.

created_record = partners.create([{"name": "James Smith"}]).select(lambda x: x.name).get()

or simply

created_ids = partners.create([{"name": "James Smith"}]).ids

Deleting Records

Deleting records can be done in 3 ways. You can either specify external_ids, database_ids, or use a filter like with search.

partners.database_ids([1, 2, 3]).delete()
partners.external_ids(["external_id_1", "external_id_2"]).delete()
partners.filter(lambda x: x.name == "John Doe").delete()

Roadmap

  • Fix no filter returning only ID on get
  • Cache Field introspection
  • Generate python stub files from field introspection to introduce "dynamic" code completion
  • Add Read Pagination support with easy itterations
  • Add support for Delete
  • Add support for Write
  • Add support for Update

Contributing

We are not open to pull requests. Create an issue to discuss pain points in the wrapper.

Disclaimer

This project is not affiliated with or endorsed by Odoo S.A. It is an independent tool designed to facilitate interaction with the Odoo API.


Happy coding! 🚀

Metadata

Release files for BetterOdooApiWrapper 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for BetterOdooApiWrapper 0.1.1
File Size Uploaded
betterodooapiwrapper-0.1.1.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for BetterOdooApiWrapper 0.1.1
File Interpreter ABI Platform
BetterOdooApiWrapper-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.2 kB

Release files / betterodooapiwrapper-0.1.1.tar.gz

Download URL betterodooapiwrapper-0.1.1.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c1fa8cb4642aca5d98a785842ae7a348436c273057fd9a2d87868ffc9b715520
BLAKE2b-256 checksum
How to use checksums
0ce0307e46d9ee394147cd43fb69067d75112dcc3e1b5ae1a4308aa60e894019
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / BetterOdooApiWrapper-0.1.1-py3-none-any.whl

Download URL BetterOdooApiWrapper-0.1.1-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
635fe737922e6b5fab9ff97234990e90fe5b801f3b1ed6214a97a1ca17850baf
BLAKE2b-256 checksum
How to use checksums
f1d94c66824d00ad8e70f9cdbe0c98d306790bf884ef3d48a7aab24a2507e04d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page