Naresh Dawer
Naresh Dawer

Reputation: 29

Return a view from a onchange function

I want to return a view from a onchange function ,but not able to redirect .. Here is my code

@api.multi
@api.onchange('product_id')
def product_id_change(self):
    model_obj = self.env['ir.model.data']
    if not self.product_id:
        return {'domain': {'product_uom': []}}

    vals = {}
    domain = {'product_uom': [('category_id', '=', self.product_id.uom_id.category_id.id)]}
    if not self.product_uom or (self.product_id.uom_id.category_id.id != self.product_uom.category_id.id):
    vals['product_uom'] = self.product_id.uom_id

    name = product.name_get()[0][1]
    if product.description_sale:
        name += '\n' + product.description_sale
    vals['name'] = name

    self._compute_tax_id()

    if self.order_id.pricelist_id and self.order_id.partner_id:
        vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)
    self.update(vals)
    data_id = model_obj._get_id('hotelbeds', 'view_search_hotel')
    view_ids = model_obj.browse(data_id).res_id
    return {
            'type': 'ir.actions.act_window',
            'name': _('Hotel Search'),
            'domain': domain,
            'view_id' :view_ids,
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'hotel.search',
            'target': 'new'
            }

Kindly help with this issue ,where am i going wrong or something missing ?

Upvotes: 0

Views: 2528

Answers (1)

Majikat
Majikat

Reputation: 722

@api.onchange decorated methods are not supposed to return anything. They just edit a "in memory" version of the object you're creating or editing. Odoo does not expect that method to return anything and will not take actions depending on the result of the method.

Best way to achieve what you're doing is to put a button next to the select field of the product. That button will call a method that will return the corresponding action.

<record id="your_view" model="ir.ui.view">
    <field name="name">your_view_name</field>
    <field name="model">your.model</field>
    <field name="arch" type="xml">
        <field name="product_id"/>
        <button name="redirect" string="Find hotel" type="object" class="oe_highlight"/>
    </field>
</record>

And in your model file

@api.multi
def redirect(self):
    model_obj = self.env['ir.model.data']
    if not self.product_id:
        return {'domain': {'product_uom': []}}
    vals = {}
    domain = {'product_uom': [('category_id', '=', self.product_id.uom_id.category_id.id)]}
    if not self.product_uom or (self.product_id.uom_id.category_id.id != self.product_uom.category_id.id):
    vals['product_uom'] = self.product_id.uom_id
    name = product.name_get()[0][1]
    if product.description_sale:
        name += '\n' + product.description_sale
    vals['name'] = name
    self._compute_tax_id()
    if self.order_id.pricelist_id and self.order_id.partner_id:
        vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)
    self.update(vals)
    data_id = model_obj._get_id('hotelbeds', 'view_search_hotel')
    view_ids = model_obj.browse(data_id).res_id
    return {
            'type': 'ir.actions.act_window',
            'name': _('Hotel Search'),
            'domain': domain,
            'view_id' :view_ids,
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'hotel.search',
            'target': 'new'
            }

Upvotes: 3

Related Questions