Odoo Help

Welcome!

This community 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.

0

Filter a one2many field with a search view

By
Jean-Claude Rieth
on 6/21/16, 3:18 PM 492 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)]"/>

         </search>

     </field>

</record>


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.

0
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"/>


Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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