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