Ir al contenido
Menú
Se marcó esta pregunta

Hi,

How can I show or hide a server action button based on a field value in my model?

For example, if the state field is 'closed', I want the "Open" button to be visible.

And if the state is 'open', I want the "Close" button to be displayed instead.

Is there a way to conditionally show or hide these server actions depending on the record's state?


<record id="mymodel_open_action_server" model="ir.actions.server">

<field name="name">Open mymodel</field>

<field name="model_id" ref="model_mymodel"/>

<field name="binding_model_id" ref="model_mymodel"/>

<field name="binding_view_types">form</field>

<field name="state">code</field>

<field name="code">

action = records.open_action()

</field>

Avatar
Descartar
Mejor respuesta

Hey, could you share your open_action() function as well? Since you’d prefer not to use a button seeing that code will help me suggest the right approach. In general you can add an attrs to your XML to toggle visibility. For example:

attrs="{'invisible': [('state', 'in', ['sale'])]}"

This will hide the element when state is not in the “sale” stage. Of course, you can adjust the domain or use different attribute just a quick example to get you started.

Avatar
Descartar
Autor

Hi, you can see my code below for more details:

<!-- Server Action -->
<record id="mymodel_closed_action_server" model="ir.actions.server">
<field name="name">Close</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="state">code</field>
<field name="code">action = records._action_close()</field>
</record>

def _action_close(self):
for rec in self:
if rec.state != "closed":
print("My logic goes here!")
rec.state = "closed"

I once hid some action buttons by finding their generated names. Odoo assigns its own names to action buttons like button name="356". In your case, after you add your action button find the name that Odoo generated and then add XML like this:

<xpath expr="//button[@name='466']" position="attributes">
<attribute name="invisible">True</attribute>
</xpath>

I hope this helps!

Mejor respuesta

Hiii,

To show/hide buttons based on the state field in Odoo 17 Community Edition , you must use button elements inside the form view , not ir.actions.server bindings, because ir.actions.server buttons do not support dynamic visibility using attrs .

Here's the correct and clean implementation :

Model: mymodel.py

Define state , action_open , and action_close :

from odoo import models, fields class MyModel (models.Model): _name = 'mymodel' _description = 'My Model' state = fields.Selection([ ( 'open' , 'Open' ), ( 'closed' , 'Closed' ) ], default= 'open' , string= 'State' ) def action_open ( self ): for record in self: record.state = 'open' def action_close ( self ): for record in self: record.state = 'closed'

View: mymodel_form_view.xml

Inherit or define your form view and add two buttons, conditionally shown using attrs .

<record id ="view_form_mymodel" model="ir.ui.view"> <field name = "name" >mymodel.form </field> <field name = "model" >mymodel </field> <field name = "arch" type = "xml"> <form string = "My Model"> <header> <button name="action_open"

​        type="object"

​​        string="Open"

        ​invisible="state != 'closed'"/> <button name="action_close"

​        type="object"

​        string="Close"

​        invisible="state != 'open'"/> </header> <sheet> <group> <field name = "state"/> <!-- other fields --> </group> </sheet> </form > </field > </record>

i hope it is use full

Avatar
Descartar
Autor

This works with a normal <button> tag. However, in my case, I need to show or hide my custom choices Close/Open in the default server action, similar to the behavior of the Archive/Unarchive button in

Use the binding_filter_domain field (Best Practice)
From Odoo 15+, ir.actions.server supports a field called binding_filter_domain, which defines when a server action appears in the action dropdown.

Example: Show “Open” action only if state == 'closed'
<record id="mymodel_open_action_server" model="ir.actions.server">
<field name="name">Open</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="binding_view_types">form</field>
<field name="binding_filter_domain">[('state', '=', 'closed')]</field>
<field name="state">code</field>
<field name="code">
action = records.action_open()
</field>
</record>
Example: Show “Close” action only if state == 'open'
<record id="mymodel_close_action_server" model="ir.actions.server">
<field name="name">Close</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="binding_view_types">form</field>
<field name="binding_filter_domain">[('state', '=', 'open')]</field>
<field name="state">code</field>
<field name="code">
action = records.action_close()
</field>
</record>
please try this
i hoe it is use full

Publicaciones relacionadas Respuestas Vistas Actividad
2
mar 23
2374
1
ago 25
395
0
ago 25
231
0
jul 25
1233
0
jul 25
3