diff --git a/odoo/addons/test_new_api/models/test_new_api.py b/odoo/addons/test_new_api/models/test_new_api.py index 01d14777a6a..b1e2c7b8f8a 100644 --- a/odoo/addons/test_new_api/models/test_new_api.py +++ b/odoo/addons/test_new_api/models/test_new_api.py @@ -18,6 +18,7 @@ class Category(models.Model): color = fields.Integer('Color Index') parent = fields.Many2one('test_new_api.category', ondelete='cascade') parent_path = fields.Char(index=True, unaccent=False) + depth = fields.Integer(compute="_compute_depth") root_categ = fields.Many2one(_name, compute='_compute_root_categ') display_name = fields.Char(compute='_compute_display_name', recursive=True, inverse='_inverse_display_name') @@ -45,6 +46,11 @@ class Category(models.Model): current = current.parent cat.root_categ = current + @api.depends('parent_path') + def _compute_depth(self): + for cat in self: + cat.depth = cat.parent_path.count('/') - 1 + def _inverse_display_name(self): for cat in self: names = cat.display_name.split('/') diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index 5924e092fc6..b26f66d9a51 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -3063,6 +3063,24 @@ class TestParentStore(common.TransactionCase): with self.assertRaises(UserError): self.cats(1, 3).write({'parent': self.cats(9).id}) + def test_compute_depend_parent_path(self): + self.assertEqual(self.cats(7).depth, 3) + self.assertEqual(self.cats(8).depth, 3) + self.assertEqual(self.cats(9).depth, 3) + + # change parent of node to have 2 parents + self.cats(7).parent = self.cats(2) + self.assertEqual(self.cats(7).depth, 2) + + # change parent of node to root + self.cats(7).parent = False + self.assertEqual(self.cats(7).depth, 0) + + # change grand-parent of nodes + self.cats(6).parent = self.cats(0) + self.assertEqual(self.cats(8).depth, 2) + self.assertEqual(self.cats(9).depth, 2) + class TestRequiredMany2one(common.TransactionCase): diff --git a/odoo/models.py b/odoo/models.py index a1f33e714d0..f6c0fc2ed94 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -4383,11 +4383,15 @@ Fields: FROM {0} node WHERE node.id IN %s AND child.parent_path LIKE concat(node.parent_path, '%%') - RETURNING child.id + RETURNING child.id, child.parent_path """ cr.execute(query.format(self._table), [prefix, tuple(self.ids)]) - modified_ids = {row[0] for row in cr.fetchall()} - self.browse(modified_ids).modified(['parent_path']) + + # update the cache of updated nodes, and determine what to recompute + updated = dict(cr.fetchall()) + records = self.browse(updated) + self.env.cache.update(records, self._fields['parent_path'], updated.values()) + records.modified(['parent_path']) def _load_records_write(self, values): self.write(values)