[FIX] fields: make overriding a property field by a function/computed field work

Contribution by Adrien Peiffer (ACSONE).
This commit is contained in:
Raphael Collet 2015-10-23 13:46:53 +02:00
parent fa6e8448d6
commit 780cfba3c6
4 changed files with 48 additions and 2 deletions

View File

@ -1,3 +1,4 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_test_inherit_mother,access_test_inherit_mother,model_test_inherit_mother,,1,1,1,1
access_test_inherit_daughter,access_test_inherit_daughter,model_test_inherit_daughter,,1,1,1,1
access_test_inherit_property,access_test_inherit_property,model_test_inherit_property,,1,1,1,1

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_test_inherit_mother access_test_inherit_mother model_test_inherit_mother 1 1 1 1
3 access_test_inherit_daughter access_test_inherit_daughter model_test_inherit_daughter 1 1 1 1
4 access_test_inherit_property access_test_inherit_property model_test_inherit_property 1 1 1 1

View File

@ -79,4 +79,34 @@ class res_partner(models.Model):
# define a one2many field based on the inherited field partner_id
daughter_ids = fields.One2many('test.inherit.daughter', 'partner_id')
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
# 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

View File

@ -78,4 +78,18 @@ class test_inherits(common.TransactionCase):
self.assertIn(partner_demo, partners)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
class test_override_property(common.TransactionCase):
def test_override_with_function_field(self):
""" test overriding a property field by a function field """
record = self.env['test.inherit.property'].create({'name': "Stuff"})
# record.property_foo is not a property field
self.assertEqual(record.property_foo, 42)
self.assertFalse(type(record).property_foo.company_dependent)
def test_override_with_computed_field(self):
""" test overriding a property field by a computed field """
record = self.env['test.inherit.property'].create({'name': "Stuff"})
# record.property_bar is not a property field
self.assertEqual(record.property_bar, 42)
self.assertFalse(type(record).property_bar.company_dependent)

View File

@ -1399,6 +1399,7 @@ class function(_column):
def to_field_args(self):
args = super(function, self).to_field_args()
args['store'] = bool(self.store)
args['company_dependent'] = False
if self._type in ('float',):
args['digits'] = self._digits_compute or self._digits
elif self._type in ('selection', 'reference'):