Odoo supports various kinds of views to represent and manage business data in a user-friendly manner. Form view helps in representing and modifying the records of the database. For instance, when opening a customer record, product record, employee record, and sales order record, Odoo represents the record in the form of a form view.
A form view is created using XML files and the model name in Odoo. The developer can group fields together and put them in different tabs and buttons, and can make the interface according to their convenience.
In this blog, we will learn about creating a form view in Odoo 20 using a custom module.
Creating the Model
Let's start by building a very simple model named library.book.
Create a Python file within the models folder:
from odoo import fields, models
class LibraryBook(models.Model):
_name = "library.book"
_description = "Library Book"
name = fields.Char(string="Book Title", required=True)
author = fields.Char(string="Author")
isbn = fields.Char(string="ISBN")
published_date = fields.Date(string="Published Date")
price = fields.Float(string="Price")
description = fields.Text(string="Description")
In this case, we have developed a model with some essential fields, which will be displayed in our form view.
Make sure you have imported your model in models/__init__.py:
from . import library_book
Configuring Access Rights
Before the creation of the form view, we have to give access to the library.book model to the user. Access rights can be created in Odoo 20 by using an ir.access.csv file.
Create a security folder in the module and create the following file:
id,name,model_id,group_id/id,operation,domain
access_library_book_user,library_book_user,library.book,base.group_user,crud,
Creating the Form View
Now make an XML file in the view folder, like this:
views/library_book_views.xml
Add the following form view:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_library_book_form" model="ir.ui.view">
<field name="name">library.book.form</field>
<field name="model">library.book</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="name"/>
<field name="author"/>
<field name="isbn"/>
<field name="published_date"/>
<field name="price"/>
</group>
<group>
<field name="description"/>
</group>
</sheet>
</form>
</field>
</record>
</odoo>
Now let us take a look at the important parts of the definition.
The view is defined as ir.ui.view object:
<record id="view_library_book_form" model="ir.ui.view">
The model field indicates the model that will be used to create the form view:
<field name="model">library.book</field>
The actual view architecture is set within the arch attribute:
<field name="arch" type="xml">
The <form> tag is used to create the structure of the form-view.
The <sheet> tag may be used to supply the primary body of the form.
Within the sheet, the <group> tag enables fields organization into a structural form.
For instance:
<group>
<field name="name"/>
<field name="author"/>
<field name="isbn"/>
</group>
Fields may also be assigned to more than one group when the form has many kinds of data fields.
Adding Tabs to Notebooks
In case a form has more information, it is possible to use notebook tabs to divide sections.
For instance:
<notebook>
<page string="Additional Information">
<field name="description"/>
</page>
</notebook>
The <notebook> generates a tabbed container, whereas the <page> denotes an individual tab.
Our form view can thus be modified to:
<form>
<sheet>
<group>
<group>
<field name="name"/>
<field name="author"/>
<field name="isbn"/>
</group>
<group>
<field name="published_date"/>
<field name="price"/>
</group>
</group>
<notebook>
<page string="Description">
<field name="description"/>
</page>
</notebook>
</sheet>
</form>
It gives a more organized appearance to the library book template.
Creation of List View and Window Actions
In order to have a form view in the Odoo interface, we also require a list view and a window action.
The list view is:
<record id="view_library_book_list" model="ir.ui.view">
<field name="name">library.book.list</field>
<field name="model">library.book</field>
<field name="arch" type="xml">
<list>
<field name="name"/>
<field name="author"/>
<field name="isbn"/>
<field name="published_date"/>
</list>
</field>
</record>
Now develop an action:
<record id="action_library_book" model="ir.actions.act_window">
<field name="name">Library Books</field>
<field name="res_model">library.book</field>
<field name="view_mode">list,form</field>
</record>
The view_mode indicates that users have access to both the list and form views.
Finally, create a menu entry:
<menuitem id="menu_library_root" name="Library" sequence="10"/>
<!-- Books Menu -->
<menuitem id="menu_library_books" name="Books" parent="menu_library_root" action="action_library_book"
sequence="10"/>
</odoo>
The complete XML file will therefore look like:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_library_book_form" model="ir.ui.view">
<field name="name">library.book.form</field>
<field name="model">library.book</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<group>
<field name="name"/>
<field name="author"/>
<field name="isbn"/>
</group>
<group>
<field name="published_date"/>
<field name="price"/>
</group>
</group>
<notebook>
<page string="Description">
<field name="description"/>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
<record id="view_library_book_list" model="ir.ui.view">
<field name="name">library.book.list</field>
<field name="model">library.book</field>
<field name="arch" type="xml">
<list>
<field name="name"/>
<field name="author"/>
<field name="isbn"/>
<field name="published_date"/>
</list>
</field>
</record>
<!-- Window Action -->
<record id="action_library_book" model="ir.actions.act_window">
<field name="name">Library Books</field>
<field name="res_model">library.book</field>
<field name="view_mode">list,form</field>
</record>
<!-- Root Menu -->
<menuitem id="menu_library_root" name="Library" sequence="10"/>
<!-- Books Menu -->
<menuitem id="menu_library_books" name="Books" parent="menu_library_root" action="action_library_book"
sequence="10"/>
</odoo>
Adding the XML File to the Manifest
XML file has to be added in the manifest file of that module:
{
"name": "Library Management",
"version": "20.0.1.0.0",
"depends": ["base"],
"data": [
"security/ir.access.csv",
"views/library_book_views.xml",
],
"installable": True,
"application": True,
}After the inclusion of the XML file in the manifest, the Odoo server must be restarted and the module updated.
After installing the module, go to the “Library Books” menu. The list of the books will appear and by clicking on any one of the records, you will get the form view.
Forms are an integral part of Odoo module creation since forms are responsible for dealing with individual records. With the help of XML files, developers can create sections in forms, tabs, and bind the form to actions and menus.
Creation of a simple form view in Odoo 20 involves creation of an ir.ui.view record, where model and form structure should be defined with the use of XML. When the basics of the form are known, the form can be extended with new components.
To read more about How to Create Form View in Odoo 19, refer to our blog, How to Create Form View in Odoo 19.