Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
11815 Vistas

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
Descartar
Mejor respuesta

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
Descartar
Autor

Thanks. This looks useful.

Publicaciones relacionadas Respuestas Vistas Actividad
1
mar 24
5146
1
oct 22
9458
2
sept 24
4236
6
sept 17
7891
2
mar 15
9711