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