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

I have a model (like purchase.order) and it has a field One2Many to model line (like  purchase.order.line) I want to get these lines before saving the model. Can anyone help me please?

Avatar
Zrušit
Nejlepší odpověď

Hello Mouna Cherrad, for above given scenario,  you can use a compute field and calculate the values from the rows, It will be similar like purchase order's Total(amount_total) field.
for example:

quantity_total = fields.Integer(string='Quantity', compute='_quantity_total')

@api.depends('order_line.quantity')
def _quantity_total(self):
    for order in self:
        order.quantity_total = sum(order.mapped('order_line.quantity'))

Avatar
Zrušit
Nejlepší odpověď

Please give more details about what do you want to achieve. You mean before save the record, You can get them in create method from vals. You can override create method and you can get the order_line (one2many) from vals and do what ever you want and then call super method of create/write.

Avatar
Zrušit
Autor

Hello Waleed, thank you for your answer.
Yes, the scenario is as follows:
I have a field quantity in the model that should be the sum of the quantities in the rows , so when adding a new line I want to get all the quantities in the lines above even if they are not saved to propose the next quantity of the new line (the difference between the quantity in the model and the sum of the quantities in the lines). I hope it is more clear

@Atul Patel Give you the write answer.