From f9b08f09b2c66eb24be79e6df2ef7fefaa6dac73 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Thu, 24 Mar 2016 16:30:45 +0100 Subject: [PATCH] [IMP] models: make `with_env`, `sudo`, `with_context` preserve the prefetch object --- openerp/addons/base/tests/test_api.py | 5 +++++ openerp/fields.py | 6 ++---- openerp/models.py | 8 +++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/openerp/addons/base/tests/test_api.py b/openerp/addons/base/tests/test_api.py index 5bd098b1e88..6f623b3f3f4 100644 --- a/openerp/addons/base/tests/test_api.py +++ b/openerp/addons/base/tests/test_api.py @@ -366,6 +366,11 @@ class TestAPI(common.TransactionCase): diff_prefetch(partners, partners[0]) diff_prefetch(partners, partners[:10]) + # the recordset operations below should pass the prefetch object + same_prefetch(partners, partners.sudo(self.env.ref('base.user_demo'))) + same_prefetch(partners, partners.with_context(active_test=False)) + same_prefetch(partners, partners[:10].with_prefetch(partners._prefetch)) + # iterating and reading relational fields should pass the prefetch object self.assertEqual(type(partners).country_id.type, 'many2one') self.assertEqual(type(partners).bank_ids.type, 'one2many') diff --git a/openerp/fields.py b/openerp/fields.py index 62b8d47e384..9c9e6cdf7a1 100644 --- a/openerp/fields.py +++ b/openerp/fields.py @@ -1710,10 +1710,8 @@ class Many2one(_Relational): # many2one field value (id and name) depends on the current record's # access rights, and not the value's access rights. try: - # performance trick: make sure that all records prefetched with - # value will be prefetched with value_sudo - value_sudo = value.sudo().with_prefetch(value._prefetch) - return value_sudo.name_get()[0] + # performance: value.sudo() prefetches the same records as value + return value.sudo().name_get()[0] except MissingError: # Should not happen, unless the foreign key is missing. return False diff --git a/openerp/models.py b/openerp/models.py index ce0da71c226..8c9c183903e 100644 --- a/openerp/models.py +++ b/openerp/models.py @@ -5580,10 +5580,11 @@ class BaseModel(object): The new environment will not benefit from the current environment's data cache, so later data access may incur extra delays while re-fetching from the database. + The returned recordset has the same prefetch object as ``self``. :type env: :class:`~openerp.api.Environment` """ - return self._browse(self._ids, env) + return self._browse(self._ids, env, self._prefetch) def sudo(self, user=SUPERUSER_ID): """ sudo([user=SUPERUSER]) @@ -5611,6 +5612,7 @@ class BaseModel(object): re-evaluated, the new recordset will not benefit from the current environment's data cache, so later data access may incur extra delays while re-fetching from the database. + The returned recordset has the same prefetch object as ``self``. """ return self.with_env(self.env(user=user)) @@ -5630,6 +5632,10 @@ class BaseModel(object): # -> r2._context is {'key2': True} r2 = records.with_context(key2=True) # -> r2._context is {'key1': True, 'key2': True} + + .. note: + + The returned recordset has the same prefetch object as ``self``. """ context = dict(args[0] if args else self._context, **kwargs) return self.with_env(self.env(context=context))