This question has been flagged

Hello,
I need to update a field (e.g description ) in order line (e.g purchase.order.line tree view) once I select a product in inline edit mode, how can I fetch the selected product in the backend and then update the desired field (e.g description ) in the front end?.

I tried the onchange on the model purchase.order.line but it's doesn't triggered, however it's triggered when I declare the function under purchase.order model (when selecting a new product).


Model purchase.order.line:
    description
    product_id

Model purchase.order

    one2many: purchase.order.line


class purchase_ordr_line(models.Model):
    _inherit = "purchase.order.line"       

  //cannot be trigered

   @api.onchange('product_id')   

     def on_change_product(self):
          _logger.log("on_change_  {}",self.product_id)


class purchase_ordr(models.Model):
    _inherit = "purchase.order"      
   

//triggered successfully

    @api.onchange('product_id')  
     def on_change_product(self):
          _logger.log("on_change_  {}",self.product_id)


thank you,

Avatar
Discard

There is already an onchange function defined for "product_id" in purchase.order.line. Thats why your onchange is not getting triggered. Like Maulik told, if you override that function with super(), then will get the solution I think.

Best Answer

overriding the onchange_product_id() will solve problem:

class purchase_ordr_line(models.Model):

_inherit = "purchase.order.line"


@api.multi
def onchange_product_id(self, pricelist_id, product_id, qty, uom_id,
        partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
        name=False, price_unit=False, state='draft'):
        dic_res = super(purchase_ordr_line, self).onchange_product_id(pricelist_id, product_id, qty, uom_id,partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
        name=False, price_unit=False, state='draft')
       #Following the custom code:
       dic_value = dic_res['value']
       dic_value['new_field_to_update'] = new_value
       return dic_res


Avatar
Discard
Best Answer

I'm not sure if I understood the question, but you can edit a object in inline mode using the editable="top" attribute in the tree tag from xml view document.

If you want to use one field from one model from other use related attribute in your python file.

The python file:

For get the fields from one model to other model to use it you can use the "related" attribute on the field variable declaration.

order_line_id = fields.One2many(purchase.order.line, "id_of_order_from_order_line_module")
order_line_product = fields.Many2one(related="order_line_id .product_id")
order_line_description = fields.Text(related="order_line_id .description")

now you can use the fields: description and product from order line into order model.

but if that you need is just edit in the tree view just modify the xml tree view.

the xml file:


<field name="order_line_id">
                  <tree string="Order Line" editable="top">
                    <field name="product_id"/>
                    <field name="descrition"/>
                  </tree>
</field>


If you are editing in the order view, the method will trigger the onchange from order model for the context.

If you want to access the attribute, you can use the dot notation


@api.onchange('product_id')   
     def on_change_product(self):
          _logger.log("on_change_  {}",self.product_id)
           _logger.log("on_change_  {}",self.product_id.description)
Avatar
Discard
Best Answer

Salim Rahal,

You can do with the JS by doing onclick.

Thank You.

Avatar
Discard