Odoo Help


Filter a one2many field with a search view

Jean-Claude Rieth
on 6/21/16, 3:18 PM 2,324 views

In a form view of a project.project model, I display related calendar.event records through a one2many field as a kanban

I'd like to filter that kanban view, but it doesn't work as described below.

On the form, I added the following field:

<field name="event_ids" mode="kanban"/>

The kanban shows up nicely on the form. Actually, a kanban view has been applied by default: the one that has the lowest sequence id.

As the system selected a kanban view, I assumed a search view is also applied by default.

The search view with the lowest sequence id is the following:

<record model="ir.ui.view" id="view_calendar_event_mysearch">

     <field name="name">calendar.event.mysearch</field>

     <field name="model">calendar.event</field>

     <field name="arch" type="xml">

        <search string="My search view">

            <field name="allday"/>

            <filter string="Full day events" name="filter_on_fullday" domain="[('allday','=',True)]"/>




If the search view would have been called from an action, I could have indicated a default filter in the act_window, like:

<field name="context">{'search_default_filter_on_fullday': 1}</field>

which made me think I could define a context on the field, like:

<field name="event_ids" mode="kanban" context="{'search_default_filter_on_fullday': 1}"/>

I must be missing something in my reasoning, because it doesn't work.

Maybe, no search view is selected by default. Can someone confirm?

How can I force a search view? And once a search view is applied, would it use the default filter?

If there is a search view, how can I tell the search view to execute the filter?

Thank you for your help.

Jean-Claude Rieth
On 6/24/16, 5:35 PM

Here is the solution.

In order to filter a one2many field on a form, the trick is to make a computed field, from the initial one2many field.

In my case, event_ids is the field I want to filter, in order to show only full day events.

In the model, I simply added a computed field, as follows:

    event_fullday_ids = fields.One2many('calendar.event', string='Full day events', compute='_get_event_fullday_ids')

And the function is:

    def _get_event_fullday_ids(self):
        self.event_fullday_ids = self.event_ids.search([('allday', '=', True)])

Then, instead of showing

<field name="event_ids" mode="kanban"/> on the form

I put this instead:

<field name="event_fullday_ids" mode="kanban"/>

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 6/21/16, 3:18 PM
Seen: 2324 times
Last updated: 6/24/16, 5:35 PM