Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 39 ยท Views and QWeb

Form Views, Groups and Widgets

Build Odoo form views: sheet, group, notebook, header buttons and statusbar, widgets, dynamic invisible, readonly and required attributes with the modern expression syntax.

The form view is where users spend most of their time: opening an invoice, editing a contact, moving a lead along a pipeline. A good form is mostly layout tags and <field> elements, with a few attributes that react to the data. This lesson walks through the building blocks, then models the dynamic attributes with a stand-in you can run.

The Odoo XML is shown read-only. The runnable code is a small Python model of how invisible, readonly and required expressions behave.

The skeleton of a form

XML
<form string="Book">
  <header>
    <button name="action_borrow" string="Borrow" type="object"
            class="btn-primary" invisible="state != 'available'"/>
    <button name="action_return" string="Return" type="object"
            invisible="state != 'borrowed'"/>
    <field name="state" widget="statusbar"
           statusbar_visible="available,borrowed"/>
  </header>
  <sheet>
    <div class="oe_title">
      <h1><field name="name" placeholder="Book title"/></h1>
    </div>
    <group>
      <group string="Catalogue">
        <field name="isbn"/>
        <field name="author_id"/>
      </group>
      <group string="Stock">
        <field name="copies"/>
        <field name="shelf" readonly="state == 'borrowed'"/>
      </group>
    </group>
    <notebook>
      <page string="Description">
        <field name="description"/>
      </page>
      <page string="Loans" invisible="not loan_ids">
        <field name="loan_ids"/>
      </page>
    </notebook>
  </sheet>
  <chatter/>
</form>

Each tag has a job:

  • <header> holds action buttons on the left and the statusbar on the right.
  • <sheet> is the white paper area; without it the form has a plain, full-width look.
  • <group> lays fields out as label and value pairs. Two groups side by side make two columns, and a group inside a group nests.
  • <notebook> with <page> creates tabs.
  • <chatter/> shows the message log and activities. Older versions use <div class="oe_chatter"> with message follower fields; the short tag is the newer form, so check your version's documentation.

Buttons

A button has two main types:

  • type="object" calls a Python method on the current record, here action_borrow.
  • type="action" runs an action record by its external ID, for example name="%(library.action_loans)d".

A smart button (the little counters at the top of a sheet) is a button inside a <div name="button_box" class="oe_button_box"> that usually opens a filtered list.

Widgets

The widget attribute changes how a field is displayed or edited. The type of the field already picks a sensible default; you override it when you want something else.

Widget Use
statusbar A pipeline of states on a selection or many2one
many2many_tags Tag chips instead of a table
monetary An amount with its currency symbol
image An image upload and preview
priority Stars for a priority selection
badge A coloured pill for a selection
email, phone, url Clickable links

Dynamic attributes

Before Odoo 17, dynamic behaviour used attrs="{'invisible': [('state', '=', 'draft')]}" with domain syntax. Since Odoo 17 the attribute is a Python-like expression directly on the element: invisible="state == 'draft'". The same applies to readonly and required, and to column visibility in lists. The expression sees the field values of the current record and a few helpers.

An example you will write all the time:

XML
<field name="date_return" required="state == 'borrowed'"
       invisible="state == 'available'"/>

Here the return date is hidden while the book is on the shelf and required once it has been borrowed. Notice this is client-side convenience: the server does not enforce required="..." in the view. Real rules belong in Python constraints (see Models and ORM), and secret fields in access rights (Field Security and sudo).

A stand-in for dynamic attributes

The browser evaluates the expression with a sandboxed evaluator. We can imitate that with Python's eval over a limited namespace. Do not do this with user input in real code; here the expressions are our own constants.

Python Evaluate invisible, readonly and required

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.

The same idea applies to buttons in the header. This second stand-in decides which buttons a user sees and what the statusbar highlights.

Python Header buttons and the statusbar

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.

Tips for readable forms

  • Put the most important fields in the first group, above the notebook.
  • Use string on groups and pages sparingly; field labels already come from the model.
  • Use options="{'no_create': True}" on a many2one to stop users from creating records from the dropdown, and domain to limit the choices.
  • Use default_focus="1" on the field the cursor should start in.
  • Check the Developer mode Edit View screen to see the final XML after all inheritance has been applied.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which tag draws the white paper area that holds the main fields of a form?

    The sheet is the central, paper-like container. Header and chatter sit outside it.
  2. 2. In Odoo 17 and later, how do you hide a field when state is draft?

    Since Odoo 17 the invisible, readonly and required attributes take an expression directly; attrs was removed.
  3. 3. A button with type="object" and name="action_borrow" does what?

    Object buttons call a method of the model. Action buttons (type=action) open or run an action record instead.
  4. 4. Does required="state == 'borrowed'" in the view guarantee the database never holds an empty value?

    View attributes guide the user interface only. Data rules must live in the model (constraints, required fields) because other callers skip the form.

Summary

  • A form is <form> with <header>, <sheet>, <group>, <notebook> and chatter.
  • Buttons call Python methods (type="object") or run actions (type="action").
  • Widgets change how a field is shown, for example statusbar, many2many_tags and monetary.
  • Since Odoo 17, invisible, readonly and required are direct expressions on the element.
  • View attributes are not security or data rules; enforce those in the model.

Next: List, Search and Kanban Views