summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphael Collet <rco@openerp.com>2016-03-24 16:30:45 +0100
committerRaphael Collet <rco@openerp.com>2016-04-25 13:37:11 +0200
commitf9b08f09b2c66eb24be79e6df2ef7fefaa6dac73 (patch)
tree50a7ce08865e0744fec745424d6e8d25123e0e3e
parent8bc7d8565c3e4f954c479e3b51b863e4c03dc582 (diff)
[IMP] models: make `with_env`, `sudo`, `with_context` preserve the prefetch object
-rw-r--r--openerp/addons/base/tests/test_api.py5
-rw-r--r--openerp/fields.py6
-rw-r--r--openerp/models.py8
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))