This question has been flagged

I have a model linked to a sale.order through a many2one. In the form for this model I need to show the sale order lines as soon as a sale order is selected, so I used an on_change.

The setup is like this:

class Review(osv.Model):
    _columns = {
        "sale_id": fields.many2one("sale.order", "Order reference"),
        "order_line_ids": fields.related("sale_id", "order_line", type="one2many",
                                          relation="sale.order.line"),
    }

    def onchange_sale_order(self, cr, uid, ids, sale_id, context=None):
        vals = {}
        if sale_id:
            lines = self.pool.get("sale.order.line").search(cr, uid, [('order_id','=',sale_id)])
            ...
            vals["order_line_ids"] = lines
        else:
            ...
            vals["order_line_ids"] = []
        return {"value": vals}

This seems to work as I expected to (search() returns a list of ids), except for the fact that when I hit "save" the "order_line_ids" field is reset to an empty tree view.

Is this the correct way to show this type of field with an onchange method? This answer (not enough karma for links)

help.openerp.com/question/28714/how-to-insert-value-to-a-one2many-field-in-table-with-create-method/?answer=28771#post-id-28771

uses a different approach but in that case there is no related field (I don't need to modify the original field)

EDIT

I found out why it didn't save the change. It was due to un unrelated issue. I'd still like to know if this my approach is valid though.

Avatar
Discard

Daniele did you find the answer ? i have the same problem as you but couldn't find the answer ..