Skip to Content
Menu
This question has been flagged
1 Reply
2767 Views

I modified the PosOrderLine model this way:

class PosOrderLine(models.Model):
    _inherit = "pos.order.line"
    child_products = fields.Many2many(comodel_name="product.product", 
                                relation="m2m_pos_testing_child_products", 
                                column1="m2m_id",
                                column2="product_id",
                                string="Child Products")

And I extended models.Orderline:

export_as_JSON: function () {
    var orderline = _super_orderline.export_as_JSON.call(this);
    orderline.child_products = [];
    //Loop through a Collection of models.Product
    this.child_products.each(_.bind(function (item) {
        //Python: [(6, None, item_ids)]
        return orderline.child_products.push([6, false, [item.id]]);
    }, this));
    return orderline;
},

This works fine when there's only one child product. But when I add multiple child products, only the first is saved.

How do I fix that?

Avatar
Discard
Best Answer

From \Odoo ORM documentation:

(6, _, ids) replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids.

In your example you are calling this individually for each ID, which is replacing the data every cycle of the loop.

You either need to collect the ids in an array and call (6, _, ids) once OR call (5, _, _) before the loop and then call (4, id, _) in the loop.

Cheers
Jake Robinson

Avatar
Discard
Related Posts Replies Views Activity
1
Sep 23
5915
2
Sep 23
25106
2
Feb 24
3174
4
Dec 23
16375
2
Jan 19
2581