Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4358 Lượt xem

Hello,

I'm trying to calculate the cost of shipping an item in a box that can have several items. The box has a fixed shipping cost and the cost per item is calculated by dividing the shipping cost by the number of items.

I expect the cost field of the line to be updated when a new line is added before saving the form. However the value is not updated and the onchange call returns an empty value (in fiddler).

{"jsonrpc": "2.0", "id": 844836927, "result": {"value": {}}}


class Shipping(models.Model):    
    _name = 'shipping.shipping'
    shipping_cost = fields.Float(string='Shipping cost')
    shipping_line_ids = fields.One2many('shipping.line', 'shipping_id', string='Shipping items')

    @api.onchange('shipping_line_ids')
    def onchange_shipping_line_ids(self):
        number_of_items = len(self.shipping_line_ids)
     if number_of_items == 0:
            return
        line_cost = self.shipping_cost / number_of_items
        for line in self.shipping_line_ids:
            line.cost = line_cost

class ShippingLine(models.Model):
    _name = 'shipping.line'
    shipping_id = fields.Many2one('shipping.shipping', string='Shipping')
    cost = fields.Float(string='Cost')


What I am doing wrong?

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 9 22
2297
0
thg 9 20
2979
4
thg 5 18
11223
0
thg 4 16
3263
3
thg 3 16
14090