Home › Curriculum › Odoo Developer › Recordsets, CRUD, domains, env, context and sudo
The ORM · 20 min · 17–20Recordsets, CRUD, domains, env, context and sudo
Working with recordsets, create/read/write/unlink, search and domains, the environment, context and privilege escalation.
In this lesson
- Recordsets and CRUD
- search, domains and operators
- env, context, with_company and sudo
Key points
- Recordsets: iterate, |, &, -, ensure_one(), exists()
- create(vals_list), write(vals), unlink(), browse(ids)
- Domains: tuples AND-ed; prefix |, &, ! (Polish notation); any/not any (17+)
- env.user/company/context/ref; with_context; with_company; sudo() sparingly
Real-world example
A portal page showed every customer's invoices to whoever logged in. The controller used sudo().search([]) to 'fix' an AccessError. Replacing it with a search filtered on the current partner (and record rules doing their job without sudo) closed a serious data leak; the team added 'justify every sudo' to code review.
Questions and answers
How do you include archived records in a search?
with_context(active_test=False) (or add ('active','in',[True,False])).
What does env.ref('base.main_company') return?
The record with that external ID.