Working With env Variable
In the Environment, the ORM stores a variety of context data, including the database cursor (for database queries), the current user (for access permissions verification), the current context (for storing arbitrary metadata), and caches.
Every recordset has an environment, which can be accessed with env and provides access to:
- the current user (user)
- the cursor (cr)
- the superuser flag (su)
- or the context (context)
When a recordset is created from another recordset, the environment is inherited. Use Environment to get an empty recordset in a model and query that model.
> > > self.env['sale.order']
sale.order()
> > > self.env['sale.order'].search([('state','=','draft')])
sale.order(4,7,9,17,33,)
env.ref(xml_id, raise_if_not_found=True)
The record matching the XML id will be returned:
Eg:
> > > self.env.ref('base.user_admin')
res.users(2,)
Env.user
The current environment user will be returned. The output that is returned will be a record from res.users.
env.lang
The language code for the current environment will be returned. The type will be str.
env.company
It will return the current company and will be a record of model res.company.
Fallback to the current user's main company if the context (allowed company ids) is not specified. When the allowed_company_ids context key content is invalid or unauthorized, it will generate an AccessError.