[FIX] core: fix _flush_search() when _order depends on m2o

closes odoo/odoo#46365

X-original-commit: 604b6a22db8eddfb37f9be31d3826394e25bd86c
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Co-authored-by: Raphael Collet <rco@odoo.com>
Co-authored-by: Sébastien Theys <seb@odoo.com>
This commit is contained in:
Sébastien Theys
2020-02-26 17:56:32 +00:00
co-authored by Raphael Collet
parent 382f17f462
commit 5d62e4954f
4 changed files with 47 additions and 3 deletions
@@ -816,3 +816,20 @@ class ModelParentM2o(models.Model):
name = fields.Char('Name')
child_ids = fields.One2many('test_new_api.model_child_m2o', 'parent_id', string="Children")
class Country(models.Model):
_name = 'test_new_api.country'
_description = 'Country, ordered by name'
_order = 'name, id'
name = fields.Char()
class City(models.Model):
_name = 'test_new_api.city'
_description = 'City, ordered by country then name'
_order = 'country_id, name, id'
name = fields.Char()
country_id = fields.Many2one('test_new_api.country')
@@ -49,3 +49,5 @@ access_test_new_api_model_child_m2o,access_test_new_api_model_child_m2o,model_te
access_test_new_api_model_parent_m2o,access_test_new_api_model_parent_m2o,model_test_new_api_model_parent_m2o,,1,1,1,1
access_test_new_api_model_private_address_onchange,access_test_new_api_model_private_address_onchange,model_test_new_api_model_private_address_onchange,base.group_user,1,1,1,1
access_test_new_api_req_m2o_transient,access_test_new_api_req_m2o_transient,model_test_new_api_req_m2o_transient,,0,0,0,0
access_test_new_api_country,access_test_new_api_country,model_test_new_api_country,base.group_user,1,1,1,1
access_test_new_api_city,access_test_new_api_city,model_test_new_api_city,base.group_user,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
49 access_test_new_api_model_parent_m2o access_test_new_api_model_parent_m2o model_test_new_api_model_parent_m2o 1 1 1 1
50 access_test_new_api_model_private_address_onchange access_test_new_api_model_private_address_onchange model_test_new_api_model_private_address_onchange base.group_user 1 1 1 1
51 access_test_new_api_req_m2o_transient access_test_new_api_req_m2o_transient model_test_new_api_req_m2o_transient 0 0 0 0
52 access_test_new_api_country access_test_new_api_country model_test_new_api_country base.group_user 1 1 1 1
53 access_test_new_api_city access_test_new_api_city model_test_new_api_city base.group_user 1 1 1 1
@@ -1714,7 +1714,23 @@ class TestFields(TransactionCaseWithUserDemo):
self.assertEqual(record_no_bin_size.binary_computed, expected_value)
self.assertEqual(record_bin_size.binary_computed, expected_value)
def test_96_ir_rule_m2m_field(self):
def test_96_order_m2o(self):
belgium, congo = self.env['test_new_api.country'].create([
{'name': "Duchy of Brabant"},
{'name': "Congo"},
])
cities = self.env['test_new_api.city'].create([
{'name': "Brussels", 'country_id': belgium.id},
{'name': "Kinshasa", 'country_id': congo.id},
])
# cities are sorted by country_id, name
self.assertEqual(cities.sorted().mapped('name'), ["Kinshasa", "Brussels"])
# change order of countries, and check sorted()
belgium.name = "Belgium"
self.assertEqual(cities.sorted().mapped('name'), ["Brussels", "Kinshasa"])
def test_97_ir_rule_m2m_field(self):
"""Ensures m2m fields can't be read if the left records can't be read.
Also makes sure reading m2m doesn't take more queries than necessary."""
tag = self.env['test_new_api.multi.tag'].create({})
+11 -2
View File
@@ -4307,8 +4307,14 @@ Record ids: %(records)s
return order_by_clause and (' ORDER BY %s ' % order_by_clause) or ''
@api.model
def _flush_search(self, domain, fields=None, order=None):
def _flush_search(self, domain, fields=None, order=None, seen=None):
""" Flush all the fields appearing in `domain`, `fields` and `order`. """
if seen is None:
seen = set()
elif self._name in seen:
return
seen.add(self._name)
to_flush = defaultdict(set) # {model_name: field_names}
if fields:
to_flush[self._name].update(fields)
@@ -4354,8 +4360,11 @@ Record ids: %(records)s
order_spec = order or self._order
for order_part in order_spec.split(','):
order_field = order_part.split()[0]
if order_field in self._fields:
field = self._fields.get(order_field)
if field is not None:
to_flush[self._name].add(order_field)
if field.relational:
self.env[field.comodel_name]._flush_search([], seen=seen)
if self._active_name:
to_flush[self._name].add(self._active_name)