[IMP] fields: overriding a field with default=None discards its default value
This commit is contained in:
@@ -12,6 +12,7 @@ class mother(models.Model):
|
||||
}
|
||||
_defaults = {
|
||||
'name': 'Foo',
|
||||
'state': 'a',
|
||||
}
|
||||
|
||||
surname = fields.Char(compute='_compute_surname')
|
||||
@@ -43,8 +44,8 @@ class mother(models.Model):
|
||||
# extend the name field: make it required and change its default value
|
||||
name = fields.Char(required=True, default='Bar')
|
||||
|
||||
# extend the selection of the state field
|
||||
state = fields.Selection(selection_add=[('c', 'C')])
|
||||
# extend the selection of the state field, and discard its default value
|
||||
state = fields.Selection(selection_add=[('c', 'C')], default=None)
|
||||
|
||||
# override the computed field, and extend its dependencies
|
||||
@api.one
|
||||
|
||||
@@ -40,6 +40,12 @@ class test_inherits(common.TransactionCase):
|
||||
self.assertEqual(daughter._defaults.get('name'), "Baz")
|
||||
self.assertEqual(daughter.default_get(['name']), {'name': "Baz"})
|
||||
|
||||
# the field mother.state must have no default value
|
||||
field = mother._fields['state']
|
||||
self.assertFalse(field.default)
|
||||
self.assertNotIn('state', mother._defaults)
|
||||
self.assertEqual(mother.default_get(['state']), {})
|
||||
|
||||
# the field daughter.template_id should have
|
||||
# comodel_name='test.inherit.mother', string='Template', required=True
|
||||
field = daughter._fields['template_id']
|
||||
|
||||
+3
-2
@@ -454,8 +454,9 @@ class Field(object):
|
||||
if 'default' in field.args:
|
||||
# take the value, and adapt it for model._defaults
|
||||
value = field.args['default']
|
||||
self.default = default_new_to_new(self, value)
|
||||
model._defaults[name] = default_new_to_old(self, value)
|
||||
if value is not None:
|
||||
self.default = default_new_to_new(self, value)
|
||||
model._defaults[name] = default_new_to_old(self, value)
|
||||
return
|
||||
|
||||
defaults = klass.__dict__.get('_defaults') or {}
|
||||
|
||||
Reference in New Issue
Block a user