-->

Odoo/OpenERP: hiding create button from treeview

2019-02-17 01:41发布

问题:

I have a situation here. I am using OpenERP 7. I am trying to hide Create button from tree view of my products. this can be done using

<tree create="false" .....

but situation is like. i want to keep it when user opens the tree view directly from "Asset Management" Module. But hide it when i click on Reporting for treeview.

I tried to use context like this from reporting button's function:

context['prod1']='false'
ctx = dict(context)
print ctx['prod1']

return {
   'type': 'ir.actions.act_window',
   'res_model': 'product.product',
   'view_type': 'form',
   'view_mode': 'tree,form',
   'target': 'current',
   'context':ctx,
   'create':False,   
   'domain':[('id','in',domain)]
}

and in treeview form I did:

<tree create="context.get('prod1',False)"

but I get this json related error:

ERROR : SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

this stuff is working with my button but not with my tree view. I tried adding 'create':False in return too, but unable to do what I want to. What am I missing?

回答1:

Are the views your accessing the same or are they different ?

If they are different, I believe the proper way to implement your requirement is to override the relevant view with the

create="false"

property you mentioned.

From the technical memento:

View Inheritance

Existing views should be modifying through inherited views, never directly. An inherited view references its parent view using the inherit_id field, and may add or modify existing elements in the view by referencing them through XPath expressions, and specifying the appropriate position.

Hope this helps.



回答2:

I successfully solved the very same issue by using field_view_get:

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    res = models.Model.fields_view_get(self, cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
    default_type = context.get('default_type', False)
    can_create = default_type != 'out_customer_production'
    update = not can_create and view_type in ['form', 'tree']
    if update:
        doc = etree.XML(res['arch'])
        if not can_create:
            for t in doc.xpath("//"+view_type):
                t.attrib['create'] = 'false'
        res['arch'] = etree.tostring(doc)

    return res

(I left tree and form view without the create attribute)



回答3:

Don't know how to do it in python + xml either, i would stick with javascript extension, that gets data from context or from fields and disable and hide button depending on that data.