1# -*- coding: utf-8 -*- 2from odoo import models, fields, api 3from odoo.exceptions import ValidationError 4 5 6# We just create a new model 7class Unit(models.Model): 8 _name = 'test.unit' 9 _description = 'Test Unit' 10 11 name = fields.Char('Name', required=True, translate=True) 12 state = fields.Selection([('a', 'A'), ('b', 'B')], string='State') 13 surname = fields.Char(compute='_compute_surname') 14 line_ids = fields.One2many('test.unit.line', 'unit_id') 15 readonly_name = fields.Char('Readonly Name', readonly=True) 16 size = fields.Integer() 17 18 @api.depends('name') 19 def _compute_surname(self): 20 for unit in self: 21 unit.surname = unit.name or '' 22 23 24class UnitLine(models.Model): 25 _name = 'test.unit.line' 26 _description = 'Test Unit Line' 27 28 name = fields.Char('Name', required=True) 29 unit_id = fields.Many2one('test.unit', required=True) 30 31 32# We want to _inherits from the parent model and we add some fields 33# in the child object 34class Box(models.Model): 35 _name = 'test.box' 36 _inherits = {'test.unit': 'unit_id'} 37 _description = 'Test Box' 38 39 unit_id = fields.Many2one('test.unit', 'Unit', required=True, 40 ondelete='cascade') 41 field_in_box = fields.Char('Field1') 42 size = fields.Integer() 43 44 45# We add a third level of _inherits 46class Pallet(models.Model): 47 _name = 'test.pallet' 48 _inherits = {'test.box': 'box_id'} 49 _description = 'Test Pallet' 50 51 box_id = fields.Many2one('test.box', 'Box', required=True, 52 ondelete='cascade') 53 field_in_pallet = fields.Char('Field2') 54 55 56# Another model for another test suite 57class AnotherUnit(models.Model): 58 _name = 'test.another_unit' 59 _description = 'Another Test Unit' 60 61 val1 = fields.Integer('Value 1', required=True) 62 63 64# We want to _inherits from the parent model, add a field and check 65# the new field is always equals to the first one 66class AnotherBox(models.Model): 67 _name = 'test.another_box' 68 _inherits = {'test.another_unit': 'another_unit_id'} 69 _description = 'Another Test Box' 70 71 another_unit_id = fields.Many2one('test.another_unit', 'Another Unit', 72 required=True, ondelete='cascade') 73 val2 = fields.Integer('Value 2', required=True) 74 75 @api.constrains('val1', 'val2') 76 def _check(self): 77 if self.val1 != self.val2: 78 raise ValidationError("The two values must be equals") 79