Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
4295 Zobrazení

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?

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
zář 22
2224
0
zář 20
2893
4
kvě 18
11166
0
dub 16
3212
3
bře 16
14020