Odoo 17 Development Book-Working With env Variable

Working With env Variable

In the Odoo environment, the ORM (Object-Relational Mapping) stores various context data, including the database cursor for executing database queries, the current user for access permissions verification, the current context for storing arbitrary metadata, and caches.

Every recordset has an environment that can be accessed using env, providing 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 the Environment to obtain 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:

> > > self.env.ref('base.user_admin')
res.users(2,)
env.user

The current environment user will be returned, and the output will be a record from the res.users model.

env.lang

The language code for the current environment will be returned, and the type will be a string (str).

env.company

It will return the current company as a record of the res.company model.Fallback to the current user's main company if the context (allowed company ids) is not specified. If the content of the allowed_company_ids context key is invalid or unauthorized, it will generate an AccessError.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message