2014-07-06 14:44:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-16 12:34:56 +00:00
|
|
|
from openerp import models, fields, api, osv
|
2014-07-06 14:44:26 +00:00
|
|
|
|
|
|
|
# We just create a new model
|
|
|
|
class mother(models.Model):
|
|
|
|
_name = 'test.inherit.mother'
|
|
|
|
|
2014-09-16 12:34:56 +00:00
|
|
|
_columns = {
|
|
|
|
# check interoperability of field inheritance with old-style fields
|
2014-10-02 15:01:03 +00:00
|
|
|
'name': osv.fields.char('Name'),
|
2014-10-08 08:47:25 +00:00
|
|
|
'state': osv.fields.selection([('a', 'A'), ('b', 'B')], string='State'),
|
2014-09-16 12:34:56 +00:00
|
|
|
}
|
2014-10-02 15:01:03 +00:00
|
|
|
_defaults = {
|
|
|
|
'name': 'Foo',
|
|
|
|
}
|
2014-09-16 12:34:56 +00:00
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
surname = fields.Char(compute='_compute_surname')
|
|
|
|
|
|
|
|
@api.one
|
|
|
|
@api.depends('name')
|
|
|
|
def _compute_surname(self):
|
|
|
|
self.surname = self.name or ''
|
|
|
|
|
|
|
|
# We want to inherits from the parent model and we add some fields
|
|
|
|
# in the child object
|
|
|
|
class daughter(models.Model):
|
2014-08-22 08:01:41 +00:00
|
|
|
_name = 'test.inherit.daughter'
|
2014-07-06 14:44:26 +00:00
|
|
|
|
|
|
|
template_id = fields.Many2one('test.inherit.mother', 'Template',
|
2015-02-17 15:45:20 +00:00
|
|
|
delegate=True, required=True, ondelete='cascade')
|
2014-07-06 14:44:26 +00:00
|
|
|
field_in_daughter = fields.Char('Field1')
|
|
|
|
|
|
|
|
|
|
|
|
# We add a new field in the parent object. Because of a recent refactoring,
|
|
|
|
# this feature was broken.
|
|
|
|
# This test and these models try to show the bug and fix it.
|
|
|
|
class mother(models.Model):
|
|
|
|
_inherit = 'test.inherit.mother'
|
|
|
|
|
|
|
|
field_in_mother = fields.Char()
|
[FIX] expression: fix search on one2many field with inherited inverse field
Consider the following setting:
- on model A, field F is computed, stored, and depends on field G
- on model A, field one2many G to model B, with inverse field H
- on model B, field many2one H is inherited (_inherits) from model C
- on model C, field many2one H is stored
When adding records of model B, the field F must be recomputed. In order to
determine which records to recompute, one searches model A with a domain like
[(G, 'in', ids)]. In expression.py, this is resolved with an SQL query like
select H from B where id in {ids}
This query fails, since the field H is not stored in model B. This happens in
general if H is not stored (it may be any computed field). In that case, one
should instead browse records from B, and read field H through the ORM.
A test case has been added: it introduces a many2one field in a parent model,
and a one2many field using the inherited many2one on a child model. The test
checks whether one can search on the one2many field.
2015-09-04 08:06:11 +00:00
|
|
|
partner_id = fields.Many2one('res.partner')
|
2014-07-06 14:44:26 +00:00
|
|
|
|
2014-10-02 15:01:03 +00:00
|
|
|
# extend the name field: make it required and change its default value
|
|
|
|
name = fields.Char(required=True, default='Bar')
|
2014-07-06 14:44:26 +00:00
|
|
|
|
[IMP] fields: add mechanism to extend a selection field
If a selection field is defined by a list as selection, such as:
state = fields.Selection([('a', 'A'), ('b', 'B')])
one can extend it by inheritance by redefining the field, as:
state = fields.Selection(selection_add=[('c', 'C')])
The result is that the selection field will have the list
[('a', 'A'), ('b', 'B'), ('c', 'C')] as selection.
2014-07-10 20:04:03 +00:00
|
|
|
# extend the selection of the state field
|
|
|
|
state = fields.Selection(selection_add=[('c', 'C')])
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
# override the computed field, and extend its dependencies
|
|
|
|
@api.one
|
|
|
|
@api.depends('field_in_mother')
|
|
|
|
def _compute_surname(self):
|
|
|
|
if self.field_in_mother:
|
|
|
|
self.surname = self.field_in_mother
|
|
|
|
else:
|
|
|
|
super(mother, self)._compute_surname()
|
|
|
|
|
[IMP] fields: add mechanism to extend a selection field
If a selection field is defined by a list as selection, such as:
state = fields.Selection([('a', 'A'), ('b', 'B')])
one can extend it by inheritance by redefining the field, as:
state = fields.Selection(selection_add=[('c', 'C')])
The result is that the selection field will have the list
[('a', 'A'), ('b', 'B'), ('c', 'C')] as selection.
2014-07-10 20:04:03 +00:00
|
|
|
|
|
|
|
class mother(models.Model):
|
|
|
|
_inherit = 'test.inherit.mother'
|
|
|
|
|
|
|
|
# extend again the selection of the state field
|
|
|
|
state = fields.Selection(selection_add=[('d', 'D')])
|
|
|
|
|
2014-08-22 08:01:41 +00:00
|
|
|
|
|
|
|
class daughter(models.Model):
|
|
|
|
_inherit = 'test.inherit.daughter'
|
|
|
|
|
|
|
|
# simply redeclare the field without adding any option
|
|
|
|
template_id = fields.Many2one()
|
|
|
|
|
2014-11-06 08:59:41 +00:00
|
|
|
# change the default value of an inherited field
|
|
|
|
name = fields.Char(default='Baz')
|
|
|
|
|
[FIX] expression: fix search on one2many field with inherited inverse field
Consider the following setting:
- on model A, field F is computed, stored, and depends on field G
- on model A, field one2many G to model B, with inverse field H
- on model B, field many2one H is inherited (_inherits) from model C
- on model C, field many2one H is stored
When adding records of model B, the field F must be recomputed. In order to
determine which records to recompute, one searches model A with a domain like
[(G, 'in', ids)]. In expression.py, this is resolved with an SQL query like
select H from B where id in {ids}
This query fails, since the field H is not stored in model B. This happens in
general if H is not stored (it may be any computed field). In that case, one
should instead browse records from B, and read field H through the ORM.
A test case has been added: it introduces a many2one field in a parent model,
and a one2many field using the inherited many2one on a child model. The test
checks whether one can search on the one2many field.
2015-09-04 08:06:11 +00:00
|
|
|
|
|
|
|
class res_partner(models.Model):
|
|
|
|
_inherit = 'res.partner'
|
|
|
|
|
|
|
|
# define a one2many field based on the inherited field partner_id
|
|
|
|
daughter_ids = fields.One2many('test.inherit.daughter', 'partner_id')
|
|
|
|
|
2015-10-23 11:46:53 +00:00
|
|
|
|
|
|
|
# Check the overriding of property fields by non-property fields.
|
|
|
|
# Contribution by Adrien Peiffer (ACSONE).
|
|
|
|
class test_inherit_property(models.Model):
|
|
|
|
_name = 'test.inherit.property'
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'name': osv.fields.char('Name', required=True),
|
|
|
|
'property_foo': osv.fields.property(string='Foo', type='integer'),
|
|
|
|
'property_bar': osv.fields.property(string='Bar', type='integer'),
|
|
|
|
}
|
|
|
|
|
|
|
|
class test_inherit_property(models.Model):
|
|
|
|
_inherit = 'test.inherit.property'
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def _get_foo(self, name, arg):
|
|
|
|
return dict.fromkeys(self.ids, 42)
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
# override property_foo with an old-api function field
|
|
|
|
'property_foo': osv.fields.function(_get_foo, type='integer', string='Foo'),
|
|
|
|
}
|
|
|
|
|
|
|
|
# override property_bar with a new-api computed field
|
|
|
|
property_bar = fields.Integer(compute='_compute_bar', company_dependent=False)
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def _compute_bar(self):
|
|
|
|
for record in self:
|
|
|
|
record.property_bar = 42
|