How to Implement check_company=True in Odoo?
What is check_company=True?
check_company=True is an option for relational fields—typically Many2one—that tells Odoo to automatically verify company consistency between:
- the main record’s company_id, and
- the linked record’s company_id.
If a mismatch is detected when the field is set or changed, Odoo raises a ValidationError.
✅ Where to Use It
field_name = fields.Many2one(
'target.model',
string='Target',
check_company=True, # ← enables automatic company check
)
- Supported on Many2one, One2many, and Many2many (for the inverse Many2one).
- No additional code is needed—the ORM handles the check.
📌 Realistic Use Cases in Odoo
1. Prevent Cross-Company Journal Selection
class Expense(models.Model):
_name = 'hr.expense'
journal_id = fields.Many2one(
'account.journal',
string='Expense Journal',
check_company=True,
)
If a user tries to pick an Accounting Journal that belongs to a different company from the expense, Odoo immediately blocks the operation with a clear error.
2. Keep Stock Moves Company-Safe
class StockMove(models.Model):
_inherit = 'stock.move'
picking_id = fields.Many2one(
'stock.picking',
check_company=True, # picking and move must share the same company
)
Eliminates accidental cross-company stock movements.
3. Enforce Company Consistency in Custom Models
class ProjectAsset(models.Model):
_name = 'project.asset'
company_id = fields.Many2one('res.company', required=True)
analytic_account_id = fields.Many2one(
'account.analytic.account',
check_company=True,
)
Guarantees the analytic account comes from the same company as the asset.
🔍 How It Works Internally
- On write or create, the ORM compares record.company_id with linked_record.company_id.
- If either side is empty (False), the check is skipped.
- On failure, Odoo raises:
ValidationError: The selected Analytic Account does not belong to the same company.
✅ Summary Table
Option | Purpose | Typical Use Case |
check_company=True | Automatic company-consistency check | Journals, pickings, custom links |
_check_company_auto | Model-level blanket check (all links) | Entire models with many company fields |
Use check_company=True when you need per-field protection; use _check_company_auto = True when you want the model to enforce company consistency on all eligible fields.