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.

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:
-
Create a new order line
sale_order.write({
'order_line': [
Command.create({
'product_id': 1,
'product_uom_qty': 2,
'price_unit': 100.0,
}),
]
})
sale_order.write({
'order_line': [
Command.update(10, {'price_unit': 150.0}),
]
})
-
Delete an order line
sale_order.write({
'order_line': [
Command.delete(11),
]
})
-
Link a group to a user (Many2many)
user.write({
'groups_id': [
Command.link(14),
]
})
-
Unlink a group
user.write({
'groups_id': [
Command.unlink(14),
]
})
-
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.