从onchange函数返回一个视图

时间:2022-10-21 22:16:44

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

我想从onchange函数返回一个视图,但不能重定向..这是我的代码

@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 ?

请帮助解决这个问题,我哪里出错或什么遗失?

1 个解决方案

#1


2  

@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.

@ api.onchange修饰的方法不应该返回任何东西。他们只是编辑您正在创建或编辑的对象的“内存”版本。 Odoo不希望该方法返回任何内容,也不会根据方法的结果采取操作。

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'
            }

#1


2  

@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.

@ api.onchange修饰的方法不应该返回任何东西。他们只是编辑您正在创建或编辑的对象的“内存”版本。 Odoo不希望该方法返回任何内容,也不会根据方法的结果采取操作。

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'
            }