How to hide create or edit button based in some condition using python or xml ? Odoo 14

Viewed 223

In model stock.picking, I want to hide edit and create buttons based on the condition (if my field boolean : 'is_active' = False) I tried with:

   <xpath expr="/form" position="attributes">
                <attribute name="create">{'false':[('is_active', '=', False)]} 
                 </attribute>
                <attribute name="edit">{'false':[('is_active', '=', False)]} 
                </attribute>
       </xpath>

I also tried with python code using the fields_view_get function :

@api.model
def fields_view_get(self, view_id=None, view_type='form',
                    toolbar=False, submenu=False):

    res = super(StockPicking, self).fields_view_get(
        view_id=view_id, view_type=view_type,
        toolbar=toolbar, submenu=submenu)

    doc = etree.XML(res['arch'])
    if view_type == 'form':
        for node_form in doc.xpath("//form"):
            node_form.set("create", 'false')
            node_form.set("edit", 'false')
    res['arch'] = etree.tostring(doc)
    return res

This code works, but when adding my condition using:

   for rec in self:
         if view_type == 'form' and rec.is_active == True:
             .....

That doesn't enter to condition .

Any help please? How can I do it with xml or python .

Thanks.

0 Answers
Related