odoo/odoo#202400

Created by Discuss, Alexandre Kühn (aku)
Merged at c8ceda32d981da6435bac6a5846d03372c74f3ef

Statuses:

Linked pull requests
label
odoo-dev:master-model-to-data-aku
head
b59aaf37e78b04ae6fea2e05176bd7ce990c9474
merged
6 months ago by Discuss, Alexandre Kühn (aku)
odoo/odoo odoo/enterprise
master #202400 #81719

[IMP] mail: Record.toData() returns flat data

Before this commit, Record.toData() was returning shallow data of record at hand, and relations had only data id of the related record.

This had the following drawbacks:

  • a toData() of a record makes only sense with insert() on the targeted record
  • it's hard to return deep data of record, e.g. we'd want to return message data but also the author data.

This commit improves Record.toData() to return data in a flat format. For example:

Message.toData()

{
   "mail.message": [{ id: 1, body: "hello-world!", author: { id: 10, type: "partner" } }],
}

This returns the shallow data of the record, and the data can be directly feed in store.insert() rather than the dedicated model.

If we want to retrieve some deep data in record, we can either give the relations to retrieve their shallow data:

Message.toData(['author'])

{
   "mail.message": [{ id: 1, body: "hello-world!", author: { id: 10, type: "partner" } }],
   "res.partner": [{ id: 10, type: "partner", name: "John" }],
}

Note:

The store.insert() feature was a discuss feature because there's concerns about "python" models. As the flat data is meant to be used with store.insert(), this feature has been moved to base store. The logic around python models are overrides in discuss store.

One could also retrieve all data in all relations, with { depth: true }. Please be cautious about it: it can retrieve a lot of data! More data to retrive means more data to feed, which can lead to performance concerns.

odoo/enterprise#81719

Originally from #199055