[IMP] fields: improve _determine_default() and add test for inherited fields
This commit is contained in:
parent
2562b1c1ba
commit
5cc863245b
|
@ -69,4 +69,7 @@ class daughter(models.Model):
|
||||||
# simply redeclare the field without adding any option
|
# simply redeclare the field without adding any option
|
||||||
template_id = fields.Many2one()
|
template_id = fields.Many2one()
|
||||||
|
|
||||||
|
# change the default value of an inherited field
|
||||||
|
name = fields.Char(default='Baz')
|
||||||
|
|
||||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||||
|
|
|
@ -17,19 +17,25 @@ class test_inherits(common.TransactionCase):
|
||||||
|
|
||||||
def test_field_extension(self):
|
def test_field_extension(self):
|
||||||
""" check the extension of a field in an inherited model """
|
""" check the extension of a field in an inherited model """
|
||||||
# the field mother.name should inherit required=True, and have "Bar" as
|
|
||||||
# a default value
|
|
||||||
mother = self.env['test.inherit.mother']
|
mother = self.env['test.inherit.mother']
|
||||||
|
daughter = self.env['test.inherit.daughter']
|
||||||
|
|
||||||
|
# the field mother.name must have required=True and "Bar" as default
|
||||||
field = mother._fields['name']
|
field = mother._fields['name']
|
||||||
self.assertTrue(field.required)
|
self.assertTrue(field.required)
|
||||||
|
|
||||||
self.assertEqual(field.default(mother), "Bar")
|
self.assertEqual(field.default(mother), "Bar")
|
||||||
self.assertEqual(mother.default_get(['name']), {'name': "Bar"})
|
|
||||||
self.assertEqual(mother._defaults.get('name'), "Bar")
|
self.assertEqual(mother._defaults.get('name'), "Bar")
|
||||||
|
self.assertEqual(mother.default_get(['name']), {'name': "Bar"})
|
||||||
|
|
||||||
# the field daughter.template_id should inherit
|
# the field daughter.name must have required=False and "Baz" as default
|
||||||
# model_name='test.inherit.mother', string='Template', required=True
|
field = daughter._fields['name']
|
||||||
daughter = self.env['test.inherit.daughter']
|
self.assertFalse(field.required)
|
||||||
|
self.assertEqual(field.default(mother), "Baz")
|
||||||
|
self.assertEqual(daughter._defaults.get('name'), "Baz")
|
||||||
|
self.assertEqual(daughter.default_get(['name']), {'name': "Baz"})
|
||||||
|
|
||||||
|
# the field daughter.template_id should have
|
||||||
|
# comodel_name='test.inherit.mother', string='Template', required=True
|
||||||
field = daughter._fields['template_id']
|
field = daughter._fields['template_id']
|
||||||
self.assertEqual(field.comodel_name, 'test.inherit.mother')
|
self.assertEqual(field.comodel_name, 'test.inherit.mother')
|
||||||
self.assertEqual(field.string, "Template")
|
self.assertEqual(field.string, "Template")
|
||||||
|
|
|
@ -347,30 +347,36 @@ class Field(object):
|
||||||
# traverse the class hierarchy upwards, and take the first field
|
# traverse the class hierarchy upwards, and take the first field
|
||||||
# definition with a default or _defaults for self
|
# definition with a default or _defaults for self
|
||||||
for klass in cls.__mro__:
|
for klass in cls.__mro__:
|
||||||
field = klass.__dict__.get(name, self)
|
if name in klass.__dict__:
|
||||||
if not isinstance(field, type(self)):
|
field = klass.__dict__[name]
|
||||||
return # klass contains another value overridden by self
|
if not isinstance(field, type(self)):
|
||||||
|
# klass contains another value overridden by self
|
||||||
|
return
|
||||||
|
|
||||||
if 'default' in field._attrs:
|
if 'default' in field._attrs:
|
||||||
# take the default in field, and adapt it for cls._defaults
|
# take the default in field, and adapt it for cls._defaults
|
||||||
value = field._attrs['default']
|
value = field._attrs['default']
|
||||||
if callable(value):
|
if callable(value):
|
||||||
self.default = value
|
from openerp import api
|
||||||
cls._defaults[name] = lambda model, cr, uid, context: \
|
self.default = value
|
||||||
self.convert_to_write(value(model.browse(cr, uid, [], context)))
|
cls._defaults[name] = api.model(
|
||||||
else:
|
lambda recs: self.convert_to_write(value(recs))
|
||||||
self.default = lambda recs: value
|
)
|
||||||
cls._defaults[name] = value
|
else:
|
||||||
return
|
self.default = lambda recs: value
|
||||||
|
cls._defaults[name] = value
|
||||||
|
return
|
||||||
|
|
||||||
defaults = klass.__dict__.get('_defaults') or {}
|
defaults = klass.__dict__.get('_defaults') or {}
|
||||||
if name in defaults:
|
if name in defaults:
|
||||||
# take the value from _defaults, and adapt it for self.default
|
# take the value from _defaults, and adapt it for self.default
|
||||||
value = defaults[name]
|
value = defaults[name]
|
||||||
value_func = value if callable(value) else lambda *args: value
|
if callable(value):
|
||||||
|
func = lambda recs: value(recs._model, recs._cr, recs._uid, recs._context)
|
||||||
|
else:
|
||||||
|
func = lambda recs: value
|
||||||
self.default = lambda recs: self.convert_to_cache(
|
self.default = lambda recs: self.convert_to_cache(
|
||||||
value_func(recs._model, recs._cr, recs._uid, recs._context),
|
func(recs), recs, validate=False,
|
||||||
recs, validate=False,
|
|
||||||
)
|
)
|
||||||
cls._defaults[name] = value
|
cls._defaults[name] = value
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue