Skip to content
ERPForAll DocsDevelopment

    Pivot view

    A pivot view totals one or more measures across the values of a row field and a column field. An addon declares it in a view file and adds pivot to the action’s view_mode; no code is needed.

    <record id="book_pivot" model="ir.ui.view">
    <field name="name">sample_library.book.pivot</field>
    <field name="model">sample_library.book</field>
    <field name="arch" type="xml">
    <pivot string="Books">
    <field name="author_id" type="row"/>
    <field name="book_format" type="col"/>
    <field name="pages" type="measure"/>
    </pivot>
    </field>
    </record>
    <record id="book_action" model="ir.actions.act_window">
    <field name="res_model">sample_library.book</field>
    <field name="view_mode">list,form,pivot</field>
    </record>
    DeclarationMeaning
    <pivot string disable_linking display_quantity default_order>display_quantity="1" shows Count first even when measures are declared; with no measure at all, Count is the measure. disable_linking="1" turns the cell links off. default_order="pages desc" sorts the rows by that measure’s Total column at the start; the name is a declared measure or __count, the direction is asc (the default) or desc.
    <field name="f" type="row">, type="col"A grouping. Declare several, in order, up to six per axis: the first is the first level, the next opens inside it. A boolean, char, integer, selection, many2one, date or datetime field. The same field may appear twice only with different intervals.
    interval="year"On a date or datetime row or column field: day, week, month, quarter or year. A date field with no interval groups by month. Refused on any other field and on measures.
    <field name="f" type="measure">A measure, summed. An integer, float or monetary field (a monetary measure is shown as a plain number; its currency is not handled yet).

    The table has a Total row and a Total column. Every header is a button. A click on an open header folds it with everything below it; a click on a folded header expands it by the next declared grouping, or, when none is left, opens a menu of the search view’s group-by items and the model’s groupable fields (a date field offers Year, Quarter, Month, Week, Day), to any depth. The toolbar offers Measures (Count and every summable field of the model, not only the declared ones), Flip axis, Expand all and Collapse all; a click on a measure header sorts the rows (ascending, descending, none); a click on a value cell opens the list of its records, and the breadcrumb comes back to the pivot as it was left. The groupings, expanded headers, measures and sort are kept in the page address, so a reload or a shared link restores the table.

    Every header is a node (a plus square closed, a minus square opened; the whole cell is the click target, the button inside carries aria-expanded). The search menu offers its Group By section: the <filter context="{'group_by': 'field'}"> items of the model’s default search view (a date field may carry an interval: {'group_by': 'published_on:month'}), a custom group over every groupable field, and the date fields by interval. The choices set the pivot’s row groupings, in the order chosen, fully expanded, replacing what header clicks added; removing the facet returns to the arch’s rows; the columns are never changed by the search. The facet is not kept in the page address, the resulting rows are (pivot_rows, pivot_open). The client asks for the model’s default search view (get_views with [null, "search"]); the action’s search_view_id is not part of the action it receives, so a model with no default search view makes the server answer 422 “No default view of type ‘search’”, which the pivot treats as “no declared items” and still offers the custom group. A model that wants a Group By menu ships a search view (the Sample Library books do: sample_library.book.search).

    The context keys pivot_measures, pivot_row_groupby and pivot_column_groupby replace the arch’s measures and groupings in the client’s pivot configuration. The shell cannot carry them from a window action today (it refuses an action’s own context for every view), so they take effect only from the user context.

    The addon’s manifest must list web in depends: pivot is owned by web (an install without it is refused with “View-type key “pivot” belongs to addon “web""). The Sample Library addon ships a two-level pivot (author, then the year published, by format, in pages and price): Library, Books, then the Pivot button.

    The server names the attribute, the field or the limit when the view is read; nothing is silently ignored.

    DeclaredResult
    a seventh row or col fieldrefused: at most six fields per axis
    interval on a field that is not a date or datetime, on a measure, or with a value other than the fiverefused, naming the field and the interval
    a float or text field as a row or columnrefused: the table groups by boolean, char, integer, selection, many2one, date and datetime fields
    widget, invisible, options on a pivot fieldrefused by name
    default_order naming anything but a declared measure or __count, or a direction other than asc or descrefused
    a measure that is not integer, float or monetaryrefused
    the same measure, or the same grouping with the same interval, twicerefused
    count distinct, currency aggregatesnot available yet

    Settings (Settings, the web addon’s panel; read by internal users, written by administrators), each with a default, a floor and a hard bound: web.pivot_max_groups (2,000; 100 to 50,000: the most groups one grouping may return), web.pivot_max_open_headers (200; 10 to 2,000: the most headers that may stay expanded) and web.pivot_max_cells (20,000; 1,000 to 1,000,000). A change that would pass one is refused with a message that names the setting, and the table stays as it was.

    Opening reads the three settings, the model’s field list, the search view and then makes one formatted_read_grouping_sets call (every depth of the rows crossed with every depth of the columns). Expanding a header reads one call narrowed to that header, only for the sets not held yet. Folding, flipping, sorting, collapsing all and turning a read measure off or on send nothing. Expand all, a measure never read and a new search read the whole table once.

    A pivot whose state differs from the arch adds pivot_rows, pivot_cols (comma lists of field or field:interval), pivot_measures, pivot_open (the expanded headers by axis) and pivot_sort to the address. Each has a closed, bounded shape: the server’s redirect policy and the client refuse a malformed value, a state too large for the address is refused with a message when it is made, and leaving the pivot for another view drops them.

    Every pivot has a Download button (label “Download”, accessible name “Download xlsx”) that saves the table on screen as an .xlsx file: no declaration is needed. The browser posts the table as it is drawn; the server formats what it receives and never re-reads the data. It checks the session, the same origin (as every state-changing route does), the caller’s read access to the document’s model (the ordinary access refusal otherwise) and the bounds below, then answers the workbook. Unit tests of the writer: addons/web/src/pivot_xlsx.rs; of the parser: addons/web/src/pivot_export.rs; the route through the real binary: crates/erp-cli/tests/human_pivot_export.rs.

    One sheet named after the action title (at most 31 characters, without [ ] : * ? / \); the file is named after the title too, with path and control characters replaced, plus .xlsx. Header cells are merged as on screen (the corner cell over all header rows, a group header over its measure columns, the Total column over two rows); the totals (the Total row, the Total column, the Total header) are bold; row labels carry the spreadsheet’s own indent, not spaces. Integers have the format #,##0; floats and monetary values #,##0 followed by the measure’s digits. Empty cells are left empty. Column widths follow the content, between 10 and 60 characters.

    Formula injection. The rule applied is OWASP’s CSV-injection rule: a text cell whose first character is =, +, -, @, a tab or a carriage return is written as an explicit string cell with the spreadsheet’s quote-prefix flag (the stored apostrophe: the cell is text and no apostrophe is shown), and its characters are kept as posted. Every text cell, header or row label, is a string cell, and no cell of the file is a formula. The reference writes strings through a call that turns a text starting with = into a formula (docs/compare/odoo-bugs.md, BUG-120); ERPForAll does not.

    Who may download. Only an internal user (base.group_user) downloads a table. Any other session is refused with an access error before the posted document is parsed, and the read check on the table’s model runs after it with the caller’s rights. The reference route asks for a signed-in user (odoo/addons/web/controllers/pivot.py:17).

    POST /erp/pivot/export, Content-Type: application/json, a browser session, Sec-Fetch-Site: same-origin (a bearer key is refused). The body is closed: an unknown key or a value of the wrong type is refused with 422 naming its path ($.rows[1].cells[2].kind).

    { "model": "library.book", "title": "Books",
    "headers": [[{"label": "Author", "rowspan": 3}, {"label": "Total", "colspan": 4, "bold": true},
    {"label": "Total", "colspan": 2, "rowspan": 2, "bold": true}],
    [{"label": "Draft", "colspan": 2}, {"label": "Done", "colspan": 2}],
    [{"label": "Count"}, {"label": "Pages"}, {"label": "Count"}, {"label": "Pages"},
    {"label": "Count", "bold": true}, {"label": "Pages", "bold": true}]],
    "rows": [{"label": "Total", "bold": true, "cells": [{"kind": "integer", "value": 3, "bold": true},
    {"kind": "float", "value": 1.5, "digits": 2, "bold": true}]},
    {"label": "Ada", "indent": 1, "cells": [{"kind": "integer", "value": 2}, {"kind": "empty"}]}] }
    • headers is a list of header rows of any depth. Each cell goes to the next free column of its row (a cell above with a rowspan has already taken its place), exactly like an HTML table; colspan and rowspan default to 1, bold to false. Column 0 is the label column. Every row must be completely covered, and no two cells may overlap.
    • rows are the body rows; every row has the same number of cells (the data columns). indent (0 to 64) defaults to 0.
    • A cell has a kind: integer (a whole number a spreadsheet keeps exactly, up to 2^53 - 1), float or monetary (a finite value and digits, 0 to 10), or empty (no value). bold defaults to false.
    • The client builds it with pivotExportDocument(model, { model, title }) in addons/web/static/src/client/pivot.ts from the same table it draws, so a nested table (several header levels) posts the same way as slice 1’s flat one. The client test compares the result with the fixtures addons/web/static/tests/fixtures/pivot-export-*.json, which the Rust test posts to the real binary.

    Five database-scope settings of the web addon, in Settings under “Pivot download”, readable by internal users and writable by administrators (base.group_system). Each is refused below its floor and above its hard bound, and a document over a bound is refused by the name of the setting.

    SettingDefaultFloorHard boundRefusal
    web.pivot_export_max_cells (cells of the sheet: header rows and body rows times the data columns plus the label column)100,000100500,000422
    web.pivot_export_max_header_depth (header rows)16264422
    web.pivot_export_max_columns (data columns, besides the label column)16,383216,383422
    web.pivot_export_max_label_bytes (one label or the title)1,024644,096422
    web.pivot_export_max_request_bytes (the whole document)8 MiB64 KiB32 MiB413, before the document is parsed

    The column hard bound is the spreadsheet format’s limit of 16,384 columns less the label column. The cell bound counts the whole sheet, so a header cannot be made large for free, and it is checked before the header grid is built or a merge is written. The cell and byte hard bounds keep one export inside the memory of a request: the document is parsed whole and the workbook built in memory. An administrator changes them with the settings service (ir.config.setting, erp_write_values) or the Settings page.

    The document is parsed and the workbook built on the server’s blocking pool, never on its async executor; the request’s own deadline still applies to the whole call.