Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
4 Відповіді
51847 Переглядів

If we obtain a recordset of model instances (through search() or browse()), what kinds of operations are available? Can recordsets be added (set + set)? Can they be appended to (setA.append(record))?

Does the recordset behave like some well known Pythonic structure (like list)? If not, is there some kind of reference that lists what operations are and aren't available?

Аватар
Відмінити
Найкраща відповідь

Hi,

Other recordset operations:

1. sorted(): To sort a recordset

# sort records by name
recset.sorted(key=lambda r: r.name)


2. filtered(): To filter a recordset:

# only keep records whose company is the current user's
records.filtered(lambda r: r.company_id == user.company_id)

# only keep records whose partner is a company
records.filtered("partner_id.is_company")


3. mapped(): To map a recordset by applying the provided function to each record in the recordset and returns a recordset if the results are recordsets.

recordset.mapped(lambda record: record.amount + record.tax_amount)

# returns a list of name
recset.mapped('name')

# returns a recordset of partners
recset.mapped('invoice_id.partner_id')


Аватар
Відмінити

Is there any way to set a common value to a field for every record in recordset instead of doing a for loop?

Автор

To answer your question Rob, yes.

In contexts where you want the changes to end up in the database, you can call write on a recordset:

recordset.write({"field_name": some_value})

If the change shouldn't go to the database (for example if you are in a compute method or an onchange method) you can instead use update:

recordset.update({"field_name": some_value})

Careful you don't confuse this update with odoo.Command.update: they are two different things.
You can read more in the ORM docs: https://www.odoo.com/documentation/15.0/developer/reference/backend/orm.html

Найкраща відповідь

Hi, Operations on recordset are

record in recset1           # include
record not in recset1       # not include
recset1 + recset2           # extend
recset1 | recset2           # union
recset1 & recset2           # intersect
recset1 - recset2           # difference

Аватар
Відмінити
Автор

Thank you for the prompt answer. I was also wondering if there are list-like operations available for single recordsets, like recordset[3:7] to get records 3 through 7 from the recordset?

Автор

To answer my own comment-question: yes, recordsets also work as sequences, meaning you can slice and unpack them like lists:

Slicing:

first_three = recordset[:2]

fourth_and_fifth = recordset[3:5]

Unpacking:

first, second = recordset_with_exactly_two_records

first, *the_rest = recordset_with_at_least_one_record

Найкраща відповідь

Is there anyway to apply a .search([]) call in a recordset?

Аватар
Відмінити
Автор

I'm not aware of being able to do this directly. What you need to do is explicitly narrow your domain to that recordset using the recordset's 'ids' attribute. So say you have a recordset partners, and you want to find out which of those partners are suppliers. Then you would do:

self.env['res.partner'].search([ ( 'id', 'in', partners.ids ), ( 'supplier', '=', True ) ])

"search([])" inside recordset is performed by "filtered_domain" method, like this:

recordset.filtered_domain([("attribute", "=", value)])

Автор

To specify on Alexandr's response above, the 'filtered_domain' method is available in Odoo 13+, but not in earlier versions. In case that's relevant.

Related Posts Відповіді Переглядів Дія
2
груд. 22
3810
3
бер. 16
11095
1
трав. 21
4249
2
січ. 20
15780
4
жовт. 19
10057