Hello Odooers & Developers! Welcome to this Odoo technical blog, where I will demonstrate how to create a Form View in Odoo. Form views are crucial for effective data management, enabling users to create, edit, and view single records in the system.
What You Will Learn in This Guide:
- What is a Form View in Odoo?
- How to Define a Model in Odoo
- How to implement a Form View through XML
- How to define a Menu and Action to access the Form View
- How to install and test your Form View
What is a Form View in Odoo?
A Form View in Odoo is a user interface that enables users to create, edit, and view individual records of a model. It provides fields, buttons, and other elements to manage data effectively.
Step 1: Define Your Model (Python)
Let’s create a model for managing courses. In your custom module, create a file like models/learn_odoo_course.py:
.py
from odoo import models, fields
class LearnOdooCourse(models.Model):
_name = 'learn.odoo.course'
name = fields.Char(string="Name")
lesson_price = fields.Float(string="Lesson Price",required=True)
lesson_date = fields.Date(string="Lesson Date")
trainee_id = fields.Many2one("trainee.learner",string="Trainee")
Explanation:
- _name: The technical name of the model, like a table name.
- fields.Char: Text field.
- fields.Float: Decimal number.
- fields.Date: Date selector.
- fields.Many2one: Links to another model, shows a dropdown.
Step 2: Create a Form View (XML)
Create a file like views/learn_odoo_course_views.xml and define your form:
XML
<odoo>
<record id="lesson_form_view" model="ir.ui.view">
<field name="name">learn.odoo.course.form</field>
<field name="model">learn.odoo.course</field>
<field name="arch" type="xml">
<form string="Course Form">
<sheet>
<group>
<group>
<field name="name"/>
<field name="trainee_id"/>
</group>
<group>
<field name="lesson_price"/>
<field name="lesson_date"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
</odoo>
Explanation:
- <form>: Starts the form.
- <sheet>: Main body of the form.
- <group>: Arranges fields nicely in rows/columns.
- <field name="..."/>: Refers to the fields defined in your Python model.
Step 3: Define Action & Menu
To open the form from the UI, you need to define:
- An Action: tells Odoo what to do (open form/tree).
- A Menu: puts it in the menu bar.
Add the following in the same XML or a new one (e.g., learn_odoo_course_menu.xml):
XML
<odoo>
<!-- Window Action -->
<record id="lesson_action" model="ir.actions.act_window">
<field name="name">Lessons</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">learn.odoo.course</field>
<field name="view_mode">tree,form</field>
</record>
<!-- Root Menu -->
<menuitem id="learn_root_menu" name="Learn Odoo"/>
<!-- Sub Menu -->
<menuitem id="lesson_menu" name="Lessons"
parent="learn_root_menu"
action="lesson_action"/>
</odoo>
Explanation:
- view_mode="tree,form": Shows both list and form views.
- menuitem: Adds clickable menu entries in Odoo's top bar.
Step 4: Add Files to Manifest
Open __manifest__.py and include your XML file:
.py
'data': [
'views/learn_odoo_course_views.xml',
],
Step 5: Install and Test
- Go to Apps > Update App List
- Search and install your custom module
- Navigate to Learn Odoo > Lessons
- Open the form, and try creating a new record
Conclusion:
Creating a Form View in Odoo is essential for anyone developing custom modules. By setting up the model, designing the form with XML, and adding actions and menus, you know how to make a basic form where users can enter and view data.
If you want odoo technical training on any odoo version , please let us know by mail at [email protected]. Then next, our odoo expoert will conduct online or offline training with you
Devintellecs & team are odoo training providers in the USA and INDIA, so we will try our best to give the training either individal or any bulk employee company.
If you want to check your odoo technical or functional knowledge, then we have prepared the odoo EXam practice test for the odoo technical & functional people.