[IMP] test_inherit: add test case, redefine Many2one field in inherited model
[odoo/odoo.git] / openerp / addons / test_inherit / models.py
1 # -*- coding: utf-8 -*-
2 from openerp import models, fields, api
3
4 # We just create a new model
5 class mother(models.Model):
6     _name = 'test.inherit.mother'
7
8     name = fields.Char('Name', required=True)
9     surname = fields.Char(compute='_compute_surname')
10     state = fields.Selection([('a', 'A'), ('b', 'B')])
11
12     @api.one
13     @api.depends('name')
14     def _compute_surname(self):
15         self.surname = self.name or ''
16
17 # We want to inherits from the parent model and we add some fields
18 # in the child object
19 class daughter(models.Model):
20     _name = 'test.inherit.daughter'
21     _inherits = {'test.inherit.mother': 'template_id'}
22
23     template_id = fields.Many2one('test.inherit.mother', 'Template',
24                                   required=True, ondelete='cascade')
25     field_in_daughter = fields.Char('Field1')
26
27
28 # We add a new field in the parent object. Because of a recent refactoring,
29 # this feature was broken.
30 # This test and these models try to show the bug and fix it.
31 class mother(models.Model):
32     _inherit = 'test.inherit.mother'
33
34     field_in_mother = fields.Char()
35
36     # extend the name field by adding a default value
37     name = fields.Char(default='Unknown')
38
39     # extend the selection of the state field
40     state = fields.Selection(selection_add=[('c', 'C')])
41
42     # override the computed field, and extend its dependencies
43     @api.one
44     @api.depends('field_in_mother')
45     def _compute_surname(self):
46         if self.field_in_mother:
47             self.surname = self.field_in_mother
48         else:
49             super(mother, self)._compute_surname()
50
51
52 class mother(models.Model):
53     _inherit = 'test.inherit.mother'
54
55     # extend again the selection of the state field
56     state = fields.Selection(selection_add=[('d', 'D')])
57
58
59 class daughter(models.Model):
60     _inherit = 'test.inherit.daughter'
61
62     # simply redeclare the field without adding any option
63     template_id = fields.Many2one()
64
65 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: