Understanding the Role of the Command Class in Odoo 18 Development

Explore the purpose and functionality of the Command Class in Odoo 18, a crucial tool for managing relational fields in Python-based module development.

Jul 15, 2025 - 13:51
 2
Understanding the Role of the Command Class in Odoo 18 Development

Odoo for Small Business often requires efficient handling of relational fields like One2many, Many2many, and Many2one when creating, updating, or deleting related records. In earlier Odoo versions (before v14), developers used tuple-based commands like (0, 0, vals) or (4, id), which were functional but difficult to read and prone to mistakes.

With the introduction of the Command class in Odoo 14and its strong recommendation from Odoo 16 onwarddevelopers now benefit from a more Pythonic, readable, and structured way to manage relational fields. Instead of relying on numerical tuples, the Command class provides named methods that clearly represent each operation.

To use Command, import it using:

from odoo import Command

Then, you can apply it in write operations like so:

Common Command methods in Odoo 18 include:

  • Command.create(vals): Create a new related record

  • Command.update(id, vals): Update an existing related record

  • Command.delete(id): Delete a record from the database

  • Command.link(id): Link an existing record (Many2many)

  • Command.unlink(id): Unlink a record without deleting it

  • Command.clear(): Remove all linked records

Examples:

  1. Create a new order line

sale_order.write({
    'order_line': [
        Command.create({
            'product_id': 1,
            'product_uom_qty': 2,
            'price_unit': 100.0,
        }),
    ]
})
  1. Update an existing order line

sale_order.write({
    'order_line': [
        Command.update(10, {'price_unit': 150.0}),
    ]
})
  1. Delete an order line

sale_order.write({
    'order_line': [
        Command.delete(11),
    ]
})
  1. Link a group to a user (Many2many)

user.write({
    'groups_id': [
        Command.link(14),
    ]
})
  1. Unlink a group

user.write({
    'groups_id': [
        Command.unlink(14),
    ]
})
  1. Clear all order lines

sale_order.write({
    'order_line': [
        Command.clear(),
    ]
})

Benefits of Using the Command Class:

  • Better readability through named methods

  • Improved maintainability for long-term projects

  • Reduced errors by avoiding index-based tuples

  • Future compatibility with ongoing Odoo updates

In conclusion,

the Command class in Odoo 18 provides a clear and maintainable way to manage relational fields and should be preferred over the older tuple syntax for all modern Odoo development.

Booking an Implementation consultant today.

eliza claire I am currently working as a Manager at **HSXTech**, where I oversee project execution and team coordination to ensure the successful delivery of innovative tech solutions. My role involves strategic planning, client communication, and driving operational efficiency across all departments.