from odoo import api, fields, models from odoo.exceptions import ValidationError class MaintenanceEquipment(models.Model): """Link maintenance equipment to the company contact it belongs to.""" _inherit = "maintenance.equipment" customer_id = fields.Many2one( "res.partner", string="Customer Contact", domain=[("is_company", "=", True)], index=True, ondelete="restrict", ) @api.constrains("customer_id") def _check_customer_is_company(self): """Reject individual contacts: equipment belongs to a company. Raises: ValidationError: when the linked contact is not a company. """ for equipment in self: if equipment.customer_id and not equipment.customer_id.is_company: raise ValidationError( self.env._( "The customer contact of equipment %(equipment)s " "must be a company.", equipment=equipment.display_name, ) )