How to add a button or a link to re-direct to another model inside a tree view odoo 12?

Viewed 1612

I've tried to do giving widget ='url' in XML, Actually what I'm trying to do is, I have a model named A and B, A has the relation to B as One2many. So I wanna load to the Model B from the tree view in the Model A.(like from a form view when I click a button it loads the model B, like that from the tree view can I achieve?

class test(models.Model):
    _name = 'consultation'

    test_id = fields.One2many('case.sheet','consultation_id',string='Case Sheet Id')
2 Answers

You can do this with a button and an action. First, you need to add a button in your tree view:

<button type="object" name="go_to_model_B" string="To Model B" icon="fa-search"/>

Here the name prop is a method in your model A (the icon prop is optional but I think it looks better). The method goes something like this:


# Method in Model A
def go_to_model_B(self):
    name_form = _('FORM B')
    return {
        'name': NAME_FORM,
        'type': 'ir.actions.act_window',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'MODEL.B',
        'res_id': self.consultation_id.id,  # Reference to the other model
        'target': 'new',
        'view_id': self.env.ref(
            'FORM_VIEW_MODEL_B').id,
        'context': {} # Optional
    }

Maybe you want to open the form in a new tab, here is another option:

import werkzeug.utils

def go_to_model_B(self):
    url_base = "{}/web?&#id={}&view_type=form&model={}&action={}'"
    action = "MODULE.ACTION_FORM_MODEL_B" # Name of the action defined in some XML
    model_name = "MODEL.B"
    target_record_id = self.consultation_id.id
    url = url_base.format(domain, target_record_id, model_name, action)
    return werkzeug.utils.redirect(url)

Another way to do this is using ir.actions.act_url:

 def go_to_model_B(self):
    return {
        'name': "TO MODEL B"
        'type': 'ir.actions.act_url',
        'url': url,  # The url is the same as above
        'target': 'new'
    }

When you click on a line in the tree view, Odoo will load the default form view to show the corresponding line record.

The url widget uses the field value as the value of href attribute ( if the we do not specify the text attribute it will use the field value).

button element can be a children element of the list view and to show a custom form view different from the one used in the tree view, you can add a button of type object in the tree view of the test_id field and declare a method with the same name in case.sheet.

Example:

Declare the method in case.sheet model:

class CaseSheet(models.Model):
    _name = 'case.sheet'

    @api.multi
    def open_form_view(self):
        self.ensure_one()
        form_view = self.env.ref('MODULE.XML_VIEW_ID')
        return {
            'name': _('Case sheet'),
            'res_model': 'case.sheet',
            'res_id': self.id,
            'views': [(form_view.id, 'form'), ],
            'type': 'ir.actions.act_window',
            'target': 'new',
        }

Add the button to the tree view:

<field name="test_id">
    <tree editable="bottom">
        <field name="name"/>
        <button name="open_form_view" type="object" string="View" class="oe_highlight"/>
    </tree>
</field>
Related