odooodoo-10odoo-9odoo-11odoo-12

Odoo, override tree view click's action to open another model's form


The case is extremely simple, yet, I cannot find any solution.

.py
model_a:
    field_b = fields.Many2one('model_b')

.xml
<tree string="List of model_a elements">
    <field name="field_b" />

I have a list view of "model_a". I would like to redirect the user to field_b's form when he clicks on a line.

Deadly simple, but is there a way to achieve this ?


Solution

  • If anybody searches a solution for this. Here is a js snippet tested on odoo 15

    Copy it to a file and make sure to load the file via __manifest__.py

    Last thing to do: Change "from.model" and "to.model" to the one you need

    odoo.define("nona.redirectToProduct", function (require) {
        "use strict";
    
        var ListRenderer = require("web.ListRenderer");
        ListRenderer.include({
            _renderRow: function (record) {
                let row = this._super(record);
                var self = this;
                if (record.model == "from.model") {
                    row.addClass('o_list_no_open');
                    // add click event
                    row.bind({
                        click: function (ev) {
                            ev.preventDefault();
                            ev.stopPropagation();
                            self.do_action({
                                type: "ir.actions.act_window",
                                res_model: "to.model",
                                res_id: record.data.product_id,
                                views: [[false, "form"]],
                                target: "target",
                                context: record.context || {},
                            });
                        }
                    });
                }
                return row
            },
        });
    });