[IMP] core: error message for computed field left unassigned
Non-stored readonly computed fields must be assigned by compute method. Make the error message more explicit. closes odoo/odoo#56269 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
@@ -594,6 +594,33 @@ class ComputeOnchangeLine(models.Model):
|
||||
required=True, ondelete='cascade')
|
||||
|
||||
|
||||
class ComputeUnassigned(models.Model):
|
||||
_name = 'test_new_api.compute.unassigned'
|
||||
_description = "Model with computed fields left unassigned"
|
||||
|
||||
foo = fields.Char()
|
||||
bar = fields.Char(compute='_compute_bar')
|
||||
bare = fields.Char(compute='_compute_bare', readonly=False)
|
||||
bars = fields.Char(compute='_compute_bars', store=True)
|
||||
bares = fields.Char(compute='_compute_bares', readonly=False, store=True)
|
||||
|
||||
@api.depends('foo')
|
||||
def _compute_bar(self):
|
||||
pass
|
||||
|
||||
@api.depends('foo')
|
||||
def _compute_bare(self):
|
||||
pass
|
||||
|
||||
@api.depends('foo')
|
||||
def _compute_bars(self):
|
||||
pass
|
||||
|
||||
@api.depends('foo')
|
||||
def _compute_bares(self):
|
||||
pass
|
||||
|
||||
|
||||
class ModelBinary(models.Model):
|
||||
_name = 'test_new_api.model_binary'
|
||||
_description = 'Test Image field'
|
||||
|
||||
@@ -26,6 +26,7 @@ access_test_new_api_cascade,access_test_new_api_cascade,model_test_new_api_casca
|
||||
access_test_new_api_compute_readwrite,access_test_new_api_compute_readwrite,model_test_new_api_compute_readwrite,,1,1,1,1
|
||||
access_test_new_api_compute_onchange,access_test_new_api_compute_onchange,model_test_new_api_compute_onchange,,1,1,1,1
|
||||
access_test_new_api_compute_onchange_line,access_test_new_api_compute_onchange_line,model_test_new_api_compute_onchange_line,,1,1,1,1
|
||||
access_test_new_api_compute_unassigned,access_test_new_api_compute_unassigned,model_test_new_api_compute_unassigned,,1,1,1,1
|
||||
access_test_new_api_binary_svg,access_test_new_api_binary_svg,model_test_new_api_binary_svg,,1,1,1,1
|
||||
access_test_new_api_monetary_base,access_test_new_api_monetary_base,model_test_new_api_monetary_base,,1,1,1,1
|
||||
access_test_new_api_monetary_related,access_test_new_api_monetary_related,model_test_new_api_monetary_related,,1,1,1,1
|
||||
|
||||
|
@@ -615,6 +615,25 @@ class TestFields(TransactionCaseWithUserDemo):
|
||||
discussion.name = "X"
|
||||
discussion.flush()
|
||||
|
||||
def test_16_compute_unassigned(self):
|
||||
model = self.env['test_new_api.compute.unassigned']
|
||||
|
||||
# real record
|
||||
record = model.create({})
|
||||
with self.assertRaises(ValueError):
|
||||
record.bar
|
||||
self.assertEqual(record.bare, False)
|
||||
self.assertEqual(record.bars, False)
|
||||
self.assertEqual(record.bares, False)
|
||||
|
||||
# new record
|
||||
record = model.new()
|
||||
with self.assertRaises(ValueError):
|
||||
record.bar
|
||||
self.assertEqual(record.bare, False)
|
||||
self.assertEqual(record.bars, False)
|
||||
self.assertEqual(record.bares, False)
|
||||
|
||||
def test_20_float(self):
|
||||
""" test rounding of float fields """
|
||||
record = self.env['test_new_api.mixed'].create({})
|
||||
|
||||
+2
-2
@@ -966,8 +966,8 @@ class Field(MetaField('DummyField', (object,), {})):
|
||||
try:
|
||||
value = env.cache.get(record, self)
|
||||
except CacheMiss:
|
||||
if self.readonly:
|
||||
raise
|
||||
if self.readonly and not self.store:
|
||||
raise ValueError("Compute method failed to assign %s.%s" % (record, self.name))
|
||||
# fallback to null value if compute gives nothing
|
||||
value = self.convert_to_cache(False, record, validate=False)
|
||||
env.cache.set(record, self, value)
|
||||
|
||||
Reference in New Issue
Block a user