Friday, October 21, 2022
1 change · master
New functionality added to Odoo
Odoo adds a reusable internal tool that lets developers group business records more easily and consistently. This reduces duplicated logic across areas like accounting, stock, events, and eLearning, helping future changes be cleaner without changing day-to-day user workflows.
Original PR description
When looking to fetch grouped data from the database, `read_group` should work(-ish), especially with support for `array_agg`. This also means this need should be more or less solved around RPC:…
When looking to fetch grouped data from the database, `read_group` should work(-ish), especially with support for `array_agg`. This also means this need should be more or less solved around RPC: client can either `read_group` or group on the client side. This leaves a glaring hole in the fabric: there's no convenient tools to apply grouping to recordsets. `itertools.groupby` exists, but it requires that the input be grouped by the key function, and it yields an iterable of items which is not the most convenient for recordsets. This here utility: - is exclusive to recordsets - returns a mapping of grouping keys to subsets of the input recordset - keeps the prefetching of the source recordset - allows grouping on a field, or an arbitrary (callable) key - should work even on "new records" (aka should be suitable for onchanges / arbitrary compute functions) The method was made non-RPC as it seems unnecessary (and even an anti-pattern) in that context: although there is something to be said for its ability to group on non-stored fields, it seems like a much better idea for the client to `read` all the data they need, then group that client-side on whatever criteria they are interested in. A few possible points of contention: - remove `_` prefix, being callable over RPC is useless but technically doesn't hurt. - method name, `partition` seemed nice but it's usually binary (true/false), and we already have one in tools, `group_by` seems a bit too overloaded or something we might want to use for a replacement of read_group (also conflicts with itertools). "group on" is common haskell lingo, apparently. - ability to work with chained fields, for convenience compatibility with m2o, seems unnecessary.