简体   繁体   中英

Odoo dynamically select views to render

I have create 4 form view from odoo user interface. Now I want to render view conditionally based on my model data like state.

How can I achieve this? or Where I can override a method to render view conditionally?

Update

According to the comment overrides the get_view method works but to refresh the form page. When I refresh the form page my custom view applies to the form page. How Can I Fix this cache issue issue?

def get_view(
        self, view_id=None, view_type='form', **options
):
    # Call the super method to get the original view definition
    result = super().get_view(
        view_id=view_id, view_type=view_type, **options
    )
    if view_type == 'form' and 'params' in self._context and 'id' in \
            self._context['params']:
        # get record ID from _context when access Form page
        record = self.browse(self._context['params']['id'])
        view = self.env['ir.ui.view'].search(
            [('name', '=', f'My Custom View')], limit=1
        )
        result.update(
            {
                'id': view.id,
                'arch': view.arch_db,
                'model': 'custom_module.model_name',
                'models': {
                    'custom_module.model_name': model_fields,
                    f'related_model': related_model_fields
                },
            }
        )
    return result

An easy solution would be to trigger a function clicking on a button. This function could decide which xml_id view to use and then return an action to this view.

The clean solution would be to do a js class and to apply it in the view. The JS class could be responsible to chose its own OWL template depending on the data it gets from the arch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM