Skip to main content

A minimal Python ORM wrapper for the Odoo API.

Project description

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

Connecting to Odoo

from wrapper 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.

[!WARNING] This implicitly creates external_ids for all returned records, including requested related records.

# 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 odoopyorm 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! 🚀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

betterodooapiwrapper-0.1.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

BetterOdooApiWrapper-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file betterodooapiwrapper-0.1.0.tar.gz.

File metadata

  • Download URL: betterodooapiwrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for betterodooapiwrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3d8dd5f35ce00b98558b94aa5d3fe4661923b1feb7495e3a099ac9cc8bb012fe
MD5 d38141d1a1a99c08496873712c1b669b
BLAKE2b-256 c09ccf874b4210eb329e33035b2b6265986048f207ac088017e1942e57dadd4c

See more details on using hashes here.

File details

Details for the file BetterOdooApiWrapper-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for BetterOdooApiWrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf32898762dc1e9730d386b3405244b9fe48a5fe9f926cf70af7c3ed992a2860
MD5 614882c1968bb3eb75c99a046c461565
BLAKE2b-256 5d6b9a20b779aacc8c2134316e999f393fb8aac1818c5131238ffbcd06bec5a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page