Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
4336 Prikazi

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
Opusti
Related Posts Odgovori Prikazi Aktivnost
1
sep. 22
2289
0
sep. 20
2958
4
maj 18
11209
0
apr. 16
3242
3
mar. 16
14057