Open Many2one field in new Tab (Browser )from Tree View [odoo]

 We can open any Many2one Relational fields from Tree view to New Tab in browser, so that the current Tree view load time will not take much time every time.


Add Button in Tree view:

------------------------------

<button name="action_open_product_document" string="Open Product Form" 
icon="fa-external-link" type="object" 
attrs="{'invisible': [('product_id', '=', False)]}" 
help="Click to open the Product Form view as Wizard."/>


Add Method in .py file

----------------------------

import werkzeug.urls
@api.multi
def action_open_product_document(self):
ctx = self.env.context.copy()
action = {'type': 'ir.actions.act_window_close'}
if self.product_id:
ctx.update({'create': 0, 'edit': 0, 'delete': 0})
view_type = 'form'
model = 'product.product'
menu_id = self.env.ref("stock.menu_stock_root")
action_id = self.env.ref("product.product_normal_action")
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
record_url = base_url

for rec in self:
fragment = dict()
if rec.product_id:
fragment['id'] = rec.product_id.id
if action_id:
fragment['action'] = action_id.id
if model:
fragment['model'] = model
if view_type:
fragment['view_type'] = view_type
if menu_id:
fragment['menu_id'] = menu_id.id

record_url = "%s/web#%s" % (base_url, werkzeug.urls.url_encode(fragment))
action = {
'type': 'ir.actions.act_url',
'target': 'new',
'url': record_url,
'context': ctx,
}
return action


Output Result Screenshot:

--------------------------------


Other Case(Wizard Process):

Suppose you want to open same Many2one fields in Wizard kind of process then you can not write method for the same..Because method will work when record is saved.

IN that case, take one field i.e. Char with compute and use same above method but to store the URL in the Char field. Now that will make widget= 'url' in XML file.

This will work try that also.


CODE:

----------

sale_url = fields.Char(string="Sale Url", compute="_compute_sale_url")
@api.one
def _compute_sale_url(self):
"""
This method will open a Many2one field i.e. sale order in new tab
"""
ctx = self.env.context.copy()
if self.sale_order_ref:
ctx.update({'create': 0, 'edit': 0, 'delete': 0})
view_type = 'form'
model = 'sale.order'
menu_id = self.env.ref("sale.menu_sale_order")
action_id = self.env.ref("sale.action_orders")
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
record_url = base_url

for rec in self:
fragment = dict()
if rec.sale_order_ref:
fragment['id'] = rec.sale_order_ref.id
if action_id:
fragment['action'] = action_id.id
if model:
fragment['model'] = model
if view_type:
fragment['view_type'] = view_type
if menu_id:
fragment['menu_id'] = menu_id.id

record_url = "%s/web#%s" % (base_url, werkzeug.urls.url_encode(fragment))
rec.sale_url = record_url



<field name="sale_url" widget="url" alt="Click To Open SaleOrder"
 help="Click to Open Sale Order Form View in New Tab"/>

Comments

Post a Comment

Popular posts from this blog

Company dependent field is not working in RECORD-RULE DOMAIN with multi-company configured system.