pythonodooodoo-8

button invisible with condition from field


This is my py

purchase_type = fields.Selection([
    ('import', 'Import'),
    ('local', 'Local'),
], string='Purchase Type')

This is my xml

<button name="action_approve" string="Approve" type="object" states="confirm" groups="ts_addons_tbk.group_tbk_director,ts_addons_tbk.group_tbk_exim,ts_addons_tbk.group_tbk_manager"  />

I want to make the button visible only when the purchase_type is import andthe groups are ts_addons_tbk.group_tbk_director and ts_addons_tbk.group_tbk_exim. Thank you sir.


Solution

  • You can use the attrs:

    attrs="{'invisible': [('purchase_type', '!=', 'import')]}">
    

    According to the documentation (check the list view buttons link):

    states is shorthand for invisible attrs and should be a list of states, comma separated, requires that the model has a state field and that it is used in the view.

    And Odoo warns about using states in combination with attrs:

    Using states in combination with attrs may lead to unexpected results as domains are combined with a logical AND.

    Edit:

    You can remove states and use only attrs (OR operator):

    attrs="{'invisible': ['|', ('purchase_type', '!=', 'import'), ('state', '!=', 'confirm')]}"