How to Implement check_company=True in Odoo?
What is check_company=True?
- the main record’s company_id, and
- the linked record’s company_id.
✅ 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,
)
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:
✅ 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 |