Chuyển đến nội dung

Bài 23 · Views and QWeb

List, Search and Kanban Views

Define Odoo list, search and kanban views: columns, optional fields, decorations, filters, group by, domains and kanban cards, with runnable Python stand-ins for domains.

Forms edit one record. Most of the day, users scan many: a table of open orders, a board of leads, a filtered view of "my overdue tasks". Three views cover this: list, search and kanban. They share one idea you must understand well, the domain, a list of conditions that selects records.

As in the rest of this track, the Odoo XML and Python are read-only here, and the runnable parts are plain Python stand-ins.

The list view

XML
<list string="Books" default_order="name" decoration-danger="state == 'lost'"
      decoration-muted="state == 'draft'">
  <field name="name"/>
  <field name="author_id"/>
  <field name="isbn" optional="hide"/>
  <field name="state" widget="badge"/>
  <field name="copies" sum="Total copies"/>
</list>

Useful attributes:

  • optional="show" or "hide" puts a column in the column picker.
  • decoration-* (danger, warning, success, info, muted, bf, it) styles a row from an expression.
  • sum and avg add a footer total.
  • editable="bottom" or "top" turns the list into an inline editor with no form.
  • create="false", delete="false" and edit="false" remove buttons.
  • multi_edit="1" lets users edit several selected rows at once.

Domains

A domain is a list of triplets and operators. Triplets are (field, operator, value), and by default they are joined with AND.

Python
[("state", "=", "available"), ("copies", ">", 0)]            # AND
["|", ("state", "=", "lost"), ("state", "=", "draft")]        # OR
["!", ("name", "ilike", "test")]                              # NOT
[("author_id.country_id.code", "=", "FR")]                    # follow relations

Operators include =, !=, <, <=, >, >=, in, not in, like, ilike, =like, child_of and parent_of. Operators |, & and ! are written in prefix (Polish) notation: | takes the next two conditions. Recent versions also accept a newer expression syntax in some places, but the list form is everywhere in existing code.

The ORM turns a domain into SQL: Model.search(domain) returns a recordset. You will meet domains in search views, window actions (<field name="domain">), relational fields, record rules (Record Rules) and automated actions.

The search view

XML
<search string="Search books">
  <field name="name" filter_domain="['|', ('name', 'ilike', self), ('isbn', 'ilike', self)]"/>
  <field name="author_id"/>
  <filter name="available" string="Available" domain="[('state', '=', 'available')]"/>
  <filter name="low_stock" string="Low stock" domain="[('copies', '&lt;', 3)]"/>
  <separator/>
  <group expand="0" string="Group By">
    <filter name="group_state" string="Status" context="{'group_by': 'state'}"/>
    <filter name="group_author" string="Author" context="{'group_by': 'author_id'}"/>
  </group>
</search>
  • <field> entries add typing-to-search suggestions in the search bar.
  • <filter> entries are the toggles in the dropdown. Filters in the same group are joined with OR, filters in different groups with AND, and <separator/> splits groups.
  • context="{'group_by': 'state'}" is how group-by options work. In XML, the < operator must be escaped as &lt;.
  • Add context="{'search_default_available': 1}" on the action to switch a filter on when the screen opens.

Below is a stand-in that evaluates a domain against dictionaries. It mimics the three operators and the prefix notation.

Python A small domain evaluator

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each top-level term is checked with all(...), which gives the same result as Odoo's implicit &.

Group by and the kanban view

A kanban view shows records as cards, usually grouped into columns by a field such as a stage.

XML
<kanban default_group_by="state" class="o_kanban_small_column">
  <field name="name"/>
  <field name="color"/>
  <templates>
    <t t-name="card">
      <field name="name" class="fw-bold fs-5"/>
      <div><field name="author_id"/></div>
      <footer>
        <field name="copies"/>
      </footer>
    </t>
  </templates>
</kanban>

The <templates> block uses QWeb, the same templating language you will meet in reports and website pages. (In Odoo 17 and earlier the template is named kanban-box instead of card.) Group by is a server-side read_group (renamed formatted_read_group in the newest versions) that returns one group per distinct value with a count and any sums requested.

Python Group records into kanban columns

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. What does the domain [('state', '=', 'a'), ('copies', '>', 0)] mean?

    Triplets in a plain list are joined with an implicit AND.
  2. 2. How do you write an OR between two conditions in a domain?

    Operators |, & and ! come first (prefix notation): ['|', cond1, cond2].
  3. 3. Which attribute adds a footer total to a numeric list column?

    sum="Label" (and avg) show an aggregate under the column.
  4. 4. How are group-by options defined in a search view?

    A filter whose context holds group_by makes a group-by entry in the dropdown.
  5. 5. Why must the less-than sign be written < in a domain inside XML?

    XML attribute values cannot hold a raw < character, so it is escaped.

Summary

  • List views show rows with optional, decoration-*, sum and inline editing.
  • A domain is a list of (field, operator, value) triplets, AND by default, with prefix |, & and !.
  • Search views define fields, filters and group-by entries.
  • Kanban shows cards from a QWeb template, usually grouped into columns.
  • Domains are reused in actions, security rules and Python code.

Next: QWeb Templates

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.