[IMP] models: make with_env, sudo, with_context preserve the prefetch object
This commit is contained in:
@@ -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')
|
||||
|
||||
+2
-4
@@ -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
|
||||
|
||||
+7
-1
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user