OD109 private-env-shortcut¶
self._cr, self._uid and self._context are deprecated; use self.env.
| Category | Severity | Series | Needs the index |
|---|---|---|---|
| Deprecation | Warning | 19.0 and later | no |
What it reports¶
Each use of self._cr, self._uid or self._context, in a module of series 19.0 or later.
Why it matters¶
The three attributes are shortcuts for self.env.cr, self.env.uid and self.env.context. Odoo
19.0 deprecated them: they still return the same values, but each access emits a
DeprecationWarning. Odoo removes deprecated names in a later series, as 20.0 did with the
group_operator field attribute (OD108), and code that still uses one then raises
AttributeError.
Modules of earlier series are not checked. Only the attributes read on self are reported, not on
another recordset such as partner._context.
Example¶
class StockPicking(models.Model):
_inherit = "stock.picking"
def _lock_for_dispatch(self):
self._cr.execute("SELECT id FROM stock_picking WHERE id IN %s FOR UPDATE", [tuple(self.ids)])
return self.with_context(dispatched_by=self._uid)
Both self._cr and self._uid warn on every call from 19.0.
class StockPicking(models.Model):
_inherit = "stock.picking"
def _lock_for_dispatch(self):
self.env.cr.execute("SELECT id FROM stock_picking WHERE id IN %s FOR UPDATE", [tuple(self.ids)])
return self.with_context(dispatched_by=self.env.uid)
Fixing¶
oscn check --fix rewrites a read of self._cr, self._uid or self._context in a model class to
self.env.cr, self.env.uid or self.env.context. An assignment or a del, and code outside a
model class, are left as they are.
See fixing findings.