Field Access
The recordset provides an "Active Record" interface, where model fields can be read and written directly from the recordset, but only in a single-record recordset. Setting a value to a field triggers a database update.
Performing multiple read and write operations on a record set will cause an exception.
When updating multiple fields simultaneously, use the write() method
Record sets are immutable, but records of the same model can be concatenated using operators to return a new record set.
The recordset provides an "active record" interface. Model fields can be read and written directly from the recordset, but only in a single record recordset. Set a trigger for the field value to update the database.
Performing multiple read and write operations on a recordset will cause an exception.
When updating multiple fields simultaneously, please use the write() method
Record sets are immutable, but records of the same model can be joined by operators to return a new record set.

Operators
record in set: Returns the records in the current record set.
set1 <= set2, set1 < set2: Returns whether set1 is a subset of set2
set1 >= set2, set1 > set2: Returns whether set1 is a superset of set2
set1 | set2: Returns a new record set that is the union of the two sets.
set1 & set2: Returns a new record set that is the intersection of the two sets.
set1 - set2: Returns a new record set that includes only records present in set1 but not in set2.
Other Record Set Operations
filtered(): Returns records that meet the given condition.
# Keep only records of the current company user.
records.filtered(lambda r: r.company_id == user.company_id)
# Only keep records where the partner is a company.
records.filtered("partner_id.is_company")
sorted(): Returns records sorted by the given key. If no key is provided, uses default sorting.
# Sort by name key
records.sorted(key=lambda r: r.name)
mapped(): Applies the given method to records in the recordset, returning a recordset of results.
# Returns a recordset of the result of adding two records from two recordsets pairwise.
records.mapped(lambda r: r.field1 + r.field2)
You can also return the value of a field via a string
# Returns a list of names
records.mapped('name')
# Returns a recordset of partners
record.mapped('partner_id')
# Returns the set of all partner banks, with duplicates removed
record.mapped('partner_id.bank_ids')
--------Environment----------
Environment is the overall handle for operating the database in Odoo. Environment stores a lot of ORM context data: the database cursor (for database queries), the current user (for checking access rights), and the current context (for storing arbitrary data). Environment also stores caches.
All record sets have an environment, which is immutable and can be accessed through "env". It allows access to the current user (user), cursor (cr), or context (context). It provides data structures for registration access, record caching, and managing recomputations.
>>> records.env<Environment object ...>>>> records.env.userres.user(3)>>> records.env.cr<Cursor object ...)
For classes that inherit from Model, you can directly obtain Environment through self.env
In the requesting Controller, you can obtain Environment through request.env()
Obtain it through the model class or model class object: cls.env, product.env
List of some common attributes:
Return the current user
self.env.user
Return the current user ID
self.env.uid
Return the current language code
self.env.lang
Current database connection
self.env.cr
Return whether in draft mode
self.env.in_draft
Application examples
1. Use env[model] to get a class object:
self.env['ir.model'].search([('state', '!=', 'manual')])
2. Use cr to execute SQL statements:
self.env.cr.execute(query, (value,))
Changing the Environment
The Environment can be customized from a recordset.
sudo(): Create a new environment using the provided user record, # create partner object as administrator
env['res.partner'].sudo().create({'name': "A Partner"})
Return whether in 'onchange' draft mode
self.env.in_onchange
# list partners visible by the "public" user
public = env.ref('base.public_user')
env['res.partner'].sudo(public).search([])
with_context(): Accepts a single positional parameter to replace the current env context; can accept any number of keyword parameters, which are then added to the current env context.
# 查找合作伙伴,如果未找到则创建一个具有指定时区的合作伙伴
env['res.partner'].with_context(tz=a_tz).find_or_create(email_address)
——————————————----------------
<field name="name" on_change="0"/>
添加onchange="0"属性,就不会执行onchange。
