In Odoo application development, understanding the basics of Tree View and Form View is crucial for managing data effectively. These two types of views are commonly used to create user interfaces that are intuitive and support CRUD (Create, Read, Update, Delete) operations. In this article, we’ll dive into each view and learn how to use them in Odoo modules.
1. Tree View
Tree View is a list view used to display data in a tabular format. It is ideal for viewing multiple records at once and is often used as the main page for a model in Odoo.
Tree View Structure
Tree View is defined in an XML file using the <tree> element. Here’s a basic example:
<record id="view_tree_example" model="ir.ui.view">
<field name="name">example.tree.view</field>
<field name="model">model.example</field>
<field name="arch" type="xml">
<tree string="Example List">
<field name="name"/>
<field name="description"/>
<field name="status"/>
</tree>
</field>
</record>
Explanation of the Code:
• id: A unique identifier for the view.
• model: The model the view is associated with (e.g., model.example).
• <tree>: The main element defining the Tree View.
• <field>: Columns to be displayed in the table.
Advantages of Tree View:
• Provides a compact overview of data.
• Supports sorting and filtering for easier navigation.
2. Form View
Form View is used to display detailed information about a single record. It is commonly used for creating or editing data.
Form View Structure
Form View is also defined in an XML file using the <form> element. Here’s a basic example:
<record id="view_form_example" model="ir.ui.view">
<field name="name">example.form.view</field>
<field name="model">model.example</field>
<field name="arch" type="xml">
<form string="Example Form">
<sheet>
<group>
<field name="name"/>
<field name="description"/>
</group>
<group>
<field name="status"/>
</group>
</sheet>
</form>
</field>
</record>
Explanation of the Code:
• <form>: The main element defining the Form View.
• <sheet>: The primary container for organizing fields.
• <group>: Mengelompokkan field dalam form untuk pengaturan tata letak.
• <field>: Kolom yang akan ditampilkan dalam form.
Advantages of Form View:
• Displays comprehensive details for each record.
• Supports input validation and business logic for data entry.