Ayman Nedjmeddine
Ayman Nedjmeddine

Reputation: 12749

How to apply a domain to a button?

I want to pass a default domain from a button to be applied to a target action.
Something like this:

<!-- action -->
<act_window id="action_target"
            name="Project Categories"
            res_model="addon.model"
            view_mode="kanban,tree,form"/>

<!-- button -->
<button class="btn btn-primary"
        type="action"
        name="%(action_target)d"
        domain="[('field_x', '=', self.id)]">
  Do Something
</button>

The way I'm doing it right now is by triggering a function that opens a new action window:

@api.multi
def action_target(self):
  self.ensure_one()
  return {
    'type': 'ir.actions.act_window',
    'src_model': self._name,
    'res_model': 'addon.model',
    'view_mode': 'kanban,tree,form',
    'domain': [('field_x', '=', self.id)],
    'target': 'main',
  }

The unwanted behavior that comes with the way I'm doing it is that is does not keep the path to the previous window like action button does:

enter image description here

I want it stay like:

enter image description here

How can that be achieved?

Upvotes: 2

Views: 2604

Answers (2)

you should try following.

<record id="action_target" model="ir.actions.act_window">
    <field name="name">Project Categories</field>
    <field name="res_model">addon.model</field>
    <field name="view_mode">kanban,tree,form</field>
    <field name="context">{
        'search_default_field_x': [active_id],
        'default_field_x': active_id,
        }
    </field>
</record>

You can set default search values using context in action. For more details refer project module and in project module refer "Tasks" button. It's the same which you want to do here.

Upvotes: 1

Unatiel
Unatiel

Reputation: 1080

I'm not quite sure what you want to do exactly, but here we go :

If you want to open another view showing all records where field_x = self.id

Use 'search_default_field_x': self.id in the context in your action. This should open your view with a search set, looking for every record where field_x is equal to self.id. You probably need to define a search view able to search on field_x for this to work (default won't fill a field if it's not in the view).

I think this answers the question in your title and the first part of your question.

If you don't want to break the breadcrumbs :

Based on odoo 10.0 documentation :

Window Actions (ir.actions.act_window)

target (optional) whether the views should be open in the main content area (current), in full screen mode (fullscreen) or in a dialog/popup (new). Use main instead of current to clear the breadcrumbs. Defaults to current.

So, try to use current instead of new and it should do what you want to do. At least, it works in some code I wrote for my company, but this is for odoo 8. Since this is the default, this is why it works in your first snippet.

Upvotes: 1

Related Questions