Skip to Content
Menu
This question has been flagged
5 Replies
3438 Views

Hi , please i want to know how to open a form view of another model on a new tab of the the browser by clicking on a button and passing it context

Avatar
Discard
Best Answer

Hi  Dilaw Bej ,


It will open a new window or tab with the URL provided in the link. Here is a slightly refined version:

return {
​ 'name': 'Go to New Window',
​ 'res_model': 'ir.actions.act_url', 'type': 'ir.actions.act_url', 'url': link, # Your URL here
  ​ 'context': context, 'target': 'new', # Opens in a new window/tab }

A few things to note:

  • 'res_model': 'ir.actions.act_url' is not needed. The 'type': 'ir.actions.act_url' already specifies that it's a URL action.
  • 'context': ctx is not required unless you specifically need to pass context for this URL. If you need it, ensure that ctx is defined beforehand. If not, you can remove this part.

Let me know if you need further modifications!


Thank you

Avatar
Discard
Best Answer

Hi,

Let's say you have a model called model1 and you want to open a form view of another model model2 in a new tab when a button is clicked. Here are the steps:

add a button to trigger the action:

<record id="view_my_model_form" model="ir.ui.view">
    <field name="name">my.model.form</field>
    <field name="model">my.model</field>
    <field name="arch" type="xml">
        <form>
            <!-- Your existing fields go here -->
            <footer>
                <button string="Open Other Model" class="oe_highlight" type="object" name="open_other_model"/>
            </footer>
        </form>
    </field>
</record>


python :

from odoo import models, fields, api

class MyModel(models.Model):
_name =model1'

# Your existing fields go here

@api.multi
def open_other_model(self):
    # You can pass context by defining a dictionary
    context = {
        'default_field_name': 'default_value',
        'additional_context_key': 'additional_context_value',
    }

    # Open the form view of the other model in a new tab
    return {
        'name': 'Other Model Form',
        'type': 'ir.actions.act_window',
        'res_model': 'model2',
        'view_mode': 'form',
        'view_id': self.env.ref('other_module.view_other_model_form').id,  # Replace with the actual view ID
        'target': 'new',
        'context': context,
    }


Create model 2 Python and xml files 


Hope it helps

Avatar
Discard
Best Answer

Hi,

You can open record in new tab with below code if your problem still not fixed.

class YourClass1(models.Model):

    _name = 'model1'

   

    def action_open_record_new_tab(self):

        record_id = # define your record id to open record

        model = 'model2'  # Replace with the actual model name

        # Construct the URL to open the record in a new tab

        url = '/web#id=%s&model=%s' % (record_id, model)

        # You Can add context for passing values

​context = {

​ }

        # Redirect to the URL

        return {

            'type': 'ir.actions.act_url',

            'url': url,

            'target': 'new',  # Open in a new tab/window

            'context': context,

        }

 

In XML define button for method.


Thanks

Avatar
Discard
Best Answer

if partner has already you can use below code:

base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') url = f"{base_url}/web#id={partner_Id}&model=res.partner&view_type=form"

return { 'type': 'ir.actions.act_url', 'url': url, 'target': '_blank' }

here is my code in odoo 17 model res.partner. hope it be can help you!

Avatar
Discard
Author Best Answer

But in this case it will open the second model as a wizard/pop-up ans not in a browser's new tab

Avatar
Discard
Related Posts Replies Views Activity
1
Jan 24
2141
1
Jan 24
1435
0
Mar 24
1669
1
Oct 22
3300
0
Aug 22
3219