Search code examples
odooodoo-10odoo-11odoo-12

Enable search filter in act_windows


I know that we can enable the search filter like this

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

But what if I want to programmatically enable the filter? Where can I put the code to enable it?

Thanks


Solution

  • You can change your menu item's action to a server action. Any action can be referenced in menu items.

    The server action (ir.actions.server) should reference the model which should be opened by the menu. And now you have some options. Three of them are hopefully easy to understand:

    1. Use type code in the server action and call a model method. The method should return a window action in form of a dictionary. The code would look like:
    action = model.my_model_method_returning_an_action()
    
    1. Use type code in the server action and create your action on-the-fly. The code would look like:
    action = {
        'type': 'ir.actions.act_window',
        'view_type': 'form',
        'view_mode': 'tree,form',
        'res_model': 'my.model',
        'target': 'current',
    }
    if env.user in env['res.config_settings'].check_my_m2m():
        action['context'] = {'search_default_Product': 1}
    
    1. Use type code in the server action and call a prepared window action, but manipulate the context:
    action = env.ref('my.external.id.of.the.action.to.call').read()[0]
    if env.user in env['res.config_settings'].check_my_m2m():
        if 'context' in action:
            action['context'].update({'search_default_Product': 1})
        else:
            action['context'] = {'search_default_Product': 1}