Enable Dark Mode!
how-to-create-form-list-and-kanban-views-with-context-in-odoo-19.jpg
By: Shalfa P

How to Create Form, List, and Kanban Views with Context in Odoo 19

Technical Odoo 19 Views

Views play a major role in Odoo because they determine how users interact with records in the system. Depending on the requirement, users may need a detailed form view, a quick list view for multiple records, or a Kanban layout for a more visual workflow.

In this example, we’ll create a simple Product Management module in Odoo 19 and use Form, List, and Kanban views. Along with that, we'll also see how context can be used to pass values and make the behavior of the application more dynamic.

By the end, you'll have a basic structure that can be reused in your own modules.

Understanding Views in Odoo

Odoo supports multiple view types, and each serves a different purpose.

  • Form View: Used when users need to view or edit complete information for a single record.
  • List View: Displays records in a table structure, making it easier to review multiple records quickly.
  • Kanban View: Provides a card-based layout and is useful when users need a visual way of organizing records.

The choice depends on the type of workflow and how users will interact with the data.

Understanding Context in Odoo

Context in Odoo is a Python dictionary used to pass temporary information between actions, views, and models.

Some common situations where context becomes useful are:

  • Setting default values while creating records
  • Applying filters automatically
  • Grouping records dynamically
  • Passing temporary information between screens

Some commonly used naming patterns are:

  • default_fieldname > Set a default value
  • search_default_filtername > Apply a default search filter
  • group_by > Group records automatically

Step 1: Module Structure

We'll create a simple module named simple_product.

Structure:

simple_product/
+-- __init__.py
+-- __manifest__.py
+-- models/
+-- views/
+-- security/

Keeping files organized from the beginning makes future customization easier.

Step 2: Create the Model

The model contains a few basic fields such as product name, price, category, status, and description.

from odoo import models, fields, api
class SimpleProduct(models.Model):
  _name = 'simple.product'
  _description = 'Simple Product'
  name = fields.Char(required=True)
  price = fields.Float()
  category = fields.Selection([
      ('electronics', 'Electronics'),
      ('clothing', 'Clothing'),
      ('food', 'Food'),
      ('books', 'Books')
  ], required=True)
  status = fields.Selection([
      ('available', 'Available'),
      ('out_of_stock', 'Out of Stock')
  ], default='available')
  description = fields.Text()
  @api.model
  def default_get(self, fields_list):
      """Optional: demonstrate context usage"""
      res = super().default_get(fields_list)
      if self.env.context.get('default_category'):
          res['category'] = self.env.context['default_category']
      if self.env.context.get('default_status'):
          res['status'] = self.env.context['default_status']
      return res

Note about default_get()

The default_get() method is optional in this example.

Odoo automatically handles values passed using default_fieldname in the context. The method becomes useful when you need additional logic or conditions before assigning values.

Step 3: Create the Form View

The Form view is where users can create or edit complete product information.

<form>
  <sheet>
      <group>
          <field name="name"/>
          <field name="price"/>
          <field name="category"/>
          <field name="status"/>
      </group>
      <group>
          <field name="description"/>
      </group>
  </sheet>
</form>

This layout keeps related fields grouped together and easier to read.

Step 4: Create the List View

List views are useful when users want to see multiple records at once.

<list decoration-success="status == 'available'"
    decoration-danger="status == 'out_of_stock'">
  <field name="name"/>
  <field name="price"/>
  <field name="category"/>
  <field name="status"/>
</list>

Record decorations provide visual indicators so users can quickly identify the status without opening each record.

Step 5: Create the Kanban View

Kanban views are useful when records need a more visual representation.

<kanban default_group_by="category">
  <field name="name"/>
  <field name="price"/>
  <field name="category"/>
  <field name="status"/>
  <field name="description"/>
  <templates>
      <t t-name="card">
          <div class="d-flex flex-column gap-2">
              <div class="d-flex justify-content-between">
                  <strong><field name="name"/></strong>
                  <span><field name="category"/></span>
              </div>
              <div>
                  <field name="description"/>
              </div>
              <div>
                  Price: <field name="price"/>
              </div>
              <div>
                  <field name="status"
                         widget="label_selection"
                         options="{'classes': {
                             'available': 'success',
                             'out_of_stock': 'danger'
                         }}"/>
              </div>
          </div>
      </t>
  </templates>
</kanban>

A few things to keep in mind while working with Kanban views in Odoo 19:

  • Use t-name="card" for templates
  • Avoid deprecated kanban-box
  • Avoid kanban_color()
  • Declare required fields at the root level of <kanban>

These small details help avoid view errors later.

Step 6: Add an Action with Context

Actions determine how records are opened and displayed.

<record id="action_product_electronics" model="ir.actions.act_window">
  <field name="name">Electronics</field>
  <field name="res_model">simple.product</field>
  <field name="view_mode">list,form,kanban</field>
  <field name="context">
      {
          'default_category': 'electronics',
          'default_status': 'available'
      }
  </field>
  <field name="domain">[('category', '=', 'electronics')]</field>
</record>

Here:

  • The domain ensures only electronic products are shown.
  • The context automatically fills values when creating new records.

Step 7: Common Context Patterns

Default values

'context': {'default_category': 'electronics'}

Search filters

'search_default_available': 1

This requires a search view to be configured.

Domain and Context Together

The two are often used together but serve different purposes:

  • Domain > Controls which records are displayed
  • Context > Controls system behavior

Step 8: Create Menus

<menuitem id="menu_product_root" name="Products"/>
<menuitem id="menu_product_electronics"
        name="Electronics"
        parent="menu_product_root"
        action="action_product_electronics"/>

Menus provide the navigation entry point for users to access the module.

Step 9: Configure Security Access

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_simple_product_user,simple.product.user,model_simple_product,base.group_user,1,1,1,1

Without access rules, users may be unable to open or manage records.

Views are more than just screens in Odoo; they shape how users work with data every day. A well-designed Form view improves data entry, a List view helps users scan information quickly, and a Kanban view adds a more visual workflow when needed.

Context adds another layer of flexibility. Even small additions like setting default values or applying filters can make the user experience smoother and reduce repetitive work.

Once you understand how views and context work together, you can build modules that feel more practical and easier to use.

To read more about How to Create Kanban View in Odoo 19, refer to our blog How to Create Kanban View in Odoo 19.


Frequently Asked Questions

Is default_get() required for context defaults in Odoo 19?

No. Odoo 19 automatically applies values passed using default_fieldname in the context. default_get() is only needed for custom logic or conditions.

Why are my context default values not working in Odoo 19?

This usually happens because of incorrect context keys, mismatched field names, or improperly defined context in the action or view. Always use the default_fieldname format correctly.

What is the difference between domain and context in Odoo 19?

A domain controls which records are displayed, while context controls system behavior such as default values, filters, and grouping.

What is the correct Kanban structure in Odoo 19?

In Odoo 19, use t-name="card" for templates, avoid deprecated kanban-box and kanban_color(), and declare fields directly under the tag.

Why does a ParseError occur when adding menus in Odoo 19?

This usually occurs when the referenced action is missing, the action ID is incorrect, or the menu is defined before the action in the XML file.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message