Email support@vrajatechnologies.com Teams Teams Contact +91 9099928101 Whatsapp +91 73596 28466 Linkedin Linkdin

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:
  1. the main record’s company_id, and
  2. 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,

    )

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.