2

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

strike_noir
  • 4,080
  • 11
  • 57
  • 100
  • 1
    What is your full requirement? An idea is to use a server action on the menu item, which can call a method returning an action. That would be a way to dynamically set the context, but has its disadvantages, too. – CZoellner Sep 16 '19 at 07:32
  • Before opening the tree view I need to check if the current user is one of the selected user (I put this value inside many2many field res.config_settings). If true I need to disable the filter otherwise, enable the filter. – strike_noir Sep 16 '19 at 08:50
  • 1
    That should work with my idea. – CZoellner Sep 16 '19 at 09:12
  • Can you give example of how a server action on the menu item can call a method? – strike_noir Sep 16 '19 at 10:56

2 Answers2

1

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}
CZoellner
  • 13,553
  • 3
  • 25
  • 38
0

You have to write this in ir.actions.act_window so that it can enable the filter automatically.

Akshay
  • 639
  • 3
  • 8