1

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
Riajul Kashem
  • 89
  • 3
  • 13
  • 1
    `fields_view_get()` could be a starting point ;-) – CZoellner May 23 '23 at 09:34
  • 1
    @CZoellner Thanks, where I can override this method from model or controller? Would you please give me an example. – Riajul Kashem May 23 '23 at 09:36
  • 2
    `fields_view_get` was renamed to [get_view](https://stackoverflow.com/questions/74065003/fields-view-get-does-not-exist-in-odoo-16/74071828#74071828) in `odoo-16` – Kenly May 23 '23 at 09:49
  • Mr. @Kenly Thank you for the correction. Would you please give me an example how Can I Select Form view to render conditionally based model data like state. – Riajul Kashem May 23 '23 at 10:34
  • 1
    `self` is an empty recordset which means you can't use the `state` field – Kenly May 23 '23 at 11:11
  • Yes, I see. Is there any way to do this ? Any Idea how can I achieve this ? – Riajul Kashem May 23 '23 at 11:20
  • okay i take it back, `fields_view_get()` or `get_view()` is not a good starting point. – CZoellner May 23 '23 at 11:20
  • A way is to use just the capabilities of Odoo to handle attributes like invisibility, readonly und required by field values. You just have to make a more complex view. An example is the form view of the model `crm.lead` which is strongly influenced by the `type` field. [for example here](https://github.com/odoo/odoo/blob/23f67ca5ac7c0061a7697ff96eddc3f5bb37b859/addons/crm/views/crm_lead_views.xml#L95) – CZoellner May 23 '23 at 11:23
  • I updated question details. Now it's required to refresh page to apply the view, maybe the views are stored in cache. how can I fix this ? – Riajul Kashem May 23 '23 at 20:59

1 Answers1

0

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.

Levizar
  • 75
  • 1
  • 8