The environment provides contextual information used by the server. Every recordset carries its execution environment in self.env
with these attributes:
env.cr
: This is the database cursor being used.env.uid
: This is the ID for the session user.env.user
: This is the record for the session user.env.context
: This is an immutable dictionary with a session context.The environment is immutable, and so it can't be modified. But we can create modified environments and then run actions using them. These methods can be used for that:
env.sudo(user)
: If this is provided with a user record, it returns an environment with that user. If no user is provided, the administrator superuser will be used, which allows running specific queries bypassing security rules.env.with_context(dictionary)
: This replaces the context with a new one.env.with_context(key=value,...)
: This sets values for keys in the current context.The env.ref()
function takes a string with an External ID and returns a record for it, as shown here:
>>> self.env.ref('base.user_root') res.users(1,)