This question has been flagged
1 Reply
10445 Views

Is it possible to compare fields in views using external id (not internal one). I need this because I need to hide some fields depending on another fields. I can make this work using internal database id. So if user chooses for example country (in view it checks that countries id), if id matches in view that it is compared, it shows another field. For example like this:

<field name="some_field"
    attrs="{'invisible': [('country_id','!=',10)]}"
/>

This one works, but it is not really reliable. Imagine if id would change (for example installing my module that id would be already taken by some other country), then it would show some_field when different country would be chosen. And that is not intended. So I thought about using external id, which you provide when adding data into tables in xml files. That id is static and actually should be the same for any database you would install that module (because that id is provided in module itself, not created by database automatically). Is there a way to use this same feature, but using external id? Of course maybe someone knows better approach to solve such problem? P.S. adding another field like boolean to show hide some_field is not an option, because it depends entirely on specific values that would be added on module install.

Avatar
Discard
Best Answer

You can use fields_view_get to add this functionality. For example

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    if context is None:context = {}
    res = super(your_osv_class_name, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=False)
    doc = etree.XML(res['arch'])
    nodes = doc.xpath("//field[@name='some_field']")
    id_found = self.pool.get('res.country').search(cr, uid,[('name','=','India')])[0]
    #or try to search for the id using the external id
    for node in nodes:
        node.set('attrs', "{'invisible':[('country_id','=',%s)]}"%id_found)
        setup_modifiers(node, res['fields']['some_field'])
    res['arch'] = etree.tostring(doc)
    return res
Avatar
Discard
Author

Thanks. This looks useful.