Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
1 Svar
3376 Visninger

Hi everyone

I'm trying to build a view using the fields_view_get method. To make the view adapt to the specific record i would like to hide some of the field groups by adding attrs="{'invisible'.....}".


Inspecting the view in the application reveals that the xml is built correctly, but still somehow the groups don't accept the invisible property and stay visible all the time, even if i set 'invisible' to 1.


 This here is the code:

@api.model
deffields_view_get(self, view_id, view_type=False, context=None, toolbar=False, submenu=False):
res = super(Equipment, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)print("VID: ", view_id)
    if view_type=='form':
        print("HELLO WORLD WFWEFWW")print(self.env.context)
        doc = ET.XML(res['arch'])​
        node = doc.findall(".//page[@name='additional_fields']")[0]

        cat_list = self.env['maintenance.equipment.category'].search([])

        group = ET.Element('group')node.append(group)
        for cat in cat_list:
            cat_group = ET.Element('group')
            cat_group.set('string',cat.name)
            cat_id = str(cat.id)
            invisible_string = "{'invisible':[('cat_id_nr','!=','2')]}"
            cat_group.set("attrs",invisible_string)
            group.append(cat_group)
            field_list = cat.test
            for field in field_list:
                print(field.name, "\n")
                el = ET.Element('field')
                el.set('name',field.name)
                cat_group.append(el)
            ET.indent(node, space="\t", level=0)
            res['arch'] = ET.tostring(doc)
            return res

I thank you in advance for your help!




Avatar
Kassér
Forfatter Bedste svar

Never mind, I found the solution myself. 

Instead of using attrs to set fields to invisible, fields_view_get requires one to use modifiers.

So by adding the following lines of code it worked.

modifiers = {}
#check for existing modifiers to not overrite anything
if cat_group.get("modifiers") is not None:
    modifiers = json.loads(cat_group.get("modifiers"))
    modifiers['invisible'] = [['cat_id_nr', '!=', cat_id]]
    cat_group.set("modifiers", json.dumps(modifiers))

I'm not sure if thats the proper way to do it, but it's a working way. If anyone has a better solution I would be happy to hear it. I'm still pretty new to odoo development ;)



Avatar
Kassér
Related Posts Besvarelser Visninger Aktivitet
1
sep. 23
1315
1
jun. 25
1610
3
jul. 25
3258
1
maj 25
1398
1
maj 25
1646