From 3f31081bc2351e352d8e6d8052f27a59b60adb1b Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Thu, 2 Oct 2014 13:35:00 +0200 Subject: [PATCH] [IMP] fields: make attribute 'default' callable --- openerp/addons/test_inherit/tests/test_inherit.py | 2 +- openerp/fields.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/openerp/addons/test_inherit/tests/test_inherit.py b/openerp/addons/test_inherit/tests/test_inherit.py index c1b62064288..9047c49b5c5 100644 --- a/openerp/addons/test_inherit/tests/test_inherit.py +++ b/openerp/addons/test_inherit/tests/test_inherit.py @@ -22,7 +22,7 @@ class test_inherits(common.TransactionCase): mother = self.env['test.inherit.mother'] field = mother._fields['name'] self.assertTrue(field.required) - self.assertEqual(field.default, 'Unknown') + self.assertEqual(field.default(mother), 'Unknown') # the field daugther.template_id should inherit # model_name='test.inherit.mother', string='Template', required=True diff --git a/openerp/fields.py b/openerp/fields.py index e72b6dbebd4..4a9f4a2a888 100644 --- a/openerp/fields.py +++ b/openerp/fields.py @@ -272,7 +272,7 @@ class Field(object): related = None # sequence of field names, for related fields related_sudo = True # whether related fields should be read as admin company_dependent = False # whether `self` is company-dependent (property field) - default = None # default value (literal or callable) + default = None # default(recs) returns the default value string = None # field label help = None # field tooltip @@ -312,6 +312,10 @@ class Field(object): attrs.update(self._attrs) # necessary in case self is not in cls # initialize `self` with `attrs` + if 'default' in attrs and not callable(attrs['default']): + # make default callable + value = attrs['default'] + attrs['default'] = lambda recs: value if attrs.get('compute'): # by default, computed fields are not stored, not copied and readonly attrs['store'] = attrs.get('store', False) @@ -824,8 +828,8 @@ class Field(object): def determine_default(self, record): """ determine the default value of field `self` on `record` """ - if self.default is not None: - value = self.default(record) if callable(self.default) else self.default + if self.default: + value = self.default(record) record._cache[self] = self.convert_to_cache(value, record) elif self.compute: self._compute_value(record)