old_contents: # Copyright 2015-TODAY Eficent
  # - Jordi Ballester Alomar
  # Copyright 2015-TODAY Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
  # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
  from odoo import api, fields, models
  
  
  class OperatingUnit(models.Model):
  
      _name = 'operating.unit'
      _description = 'Operating Unit'
  
      name = fields.Char('Name', required=True)
      code = fields.Char('Code', required=True)
      active = fields.Boolean('Active', default=True)
      company_id = fields.Many2one(
          'res.company', 'Company', required=True, default=lambda self:
          self.env['res.company']._company_default_get('account.account'))
      partner_id = fields.Many2one('res.partner', 'Partner', required=True)
      user_ids = fields.Many2many(
          'res.users', 'operating_unit_users_rel', 'poid', 'user_id',
          'Users Allowed',
      )
  
      _sql_constraints = [
          ('code_company_uniq', 'unique (code,company_id)',
           'The code of the operating unit must '
           'be unique per company!'),
          ('name_company_uniq', 'unique (name,company_id)',
           'The name of the operating unit must '
           'be unique per company!')
      ]
  
      @api.model
      def name_search(self, name='', args=None, operator='ilike', limit=100):
          # Make a search with default criteria
          names1 = super(models.Model, self).name_search(
              name=name, args=args, operator=operator, limit=limit)
          # Make the other search
          names2 = []
          if name:
              domain = [('code', '=ilike', name + '%')]
              names2 = self.search(domain, limit=limit).name_get()
          # Merge both results
          return list(set(names1) | set(names2))[:limit]
  
      @api.model
      def create(self, values):
          res = super(OperatingUnit, self).create(values)
          res.user_ids += self.env.user
          self.clear_caches()
          return res
  
      @api.multi
      def write(self, vals):
          self.clear_caches()
          return super(OperatingUnit, self).write(vals)
  
new_contents: # Copyright 2015-TODAY Eficent
  # - Jordi Ballester Alomar
  # Copyright 2015-TODAY Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
  # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
  from odoo import api, fields, models
  
  
  class OperatingUnit(models.Model):
  
      _name = 'operating.unit'
      _description = 'Operating Unit'
  
      name = fields.Char('Name', required=True)
      code = fields.Char('Code', required=True)
      active = fields.Boolean('Active', default=True)
      company_id = fields.Many2one(
          'res.company', 'Company', required=True, readonly=True,
          default=lambda self:
          self.env['res.company']._company_default_get('account.account'))
      partner_id = fields.Many2one('res.partner', 'Partner', required=True)
      user_ids = fields.Many2many(
          'res.users', 'operating_unit_users_rel', 'poid', 'user_id',
          'Users Allowed',
      )
  
      _sql_constraints = [
          ('code_company_uniq', 'unique (code,company_id)',
           'The code of the operating unit must '
           'be unique per company!'),
          ('name_company_uniq', 'unique (name,company_id)',
           'The name of the operating unit must '
           'be unique per company!')
      ]
  
      @api.model
      def name_search(self, name='', args=None, operator='ilike', limit=100):
          # Make a search with default criteria
          names1 = super(models.Model, self).name_search(
              name=name, args=args, operator=operator, limit=limit)
          # Make the other search
          names2 = []
          if name:
              domain = [('code', '=ilike', name + '%')]
              names2 = self.search(domain, limit=limit).name_get()
          # Merge both results
          return list(set(names1) | set(names2))[:limit]
  
      @api.model
      def create(self, values):
          res = super(OperatingUnit, self).create(values)
          res.user_ids += self.env.user
          self.clear_caches()
          return res
  
      @api.multi
      def write(self, vals):
          self.clear_caches()
          return super(OperatingUnit, self).write(vals)
  
subject: Make company_id readonly to avoid crashes in multi-company environments