This question has been flagged
1 Reply
3178 Views

i create a custom field x_line_total in Purchase order Line and create another custom field x_grand_total for show sum of x_line_total  field. How to compute x_line_total field into x_grand_totalfield?

This Code i written on .py file but this is not correct

class PurchaseOrder(models.Model):
_inherit = "purchase.order" 

@api.depends('order_line.x_line_total')
def _compute_x_grand_total(self): 

 for line in order.order_line: 
x_grand_total += line.x_line_total 

x_grand_total = fields.Float(compute='_compute_x_grand_total', string='Grand Total')

Avatar
Discard
Best Answer

Hi!

Change the code like this :

class PurchaseOrder(models.Model):
_inherit = "purchase.order"

x_grand_total = fields.Float(compute='_compute_x_grand_total', string='Grand Total')

@api.depends('order_line')
def _compute_x_grand_total(self):
self.x_grand_total = sum(line.x_line_total for line in self.order_line)

Best regards!

Avatar
Discard
Author

i tried your code but this error has been occurred

self.x_grand_total = sum(line.x_line_total for line in self.order_line)

AttributeError: 'purchase.order.line' object has no attribute 'x_line_total'

Hello,

Thanks it works :)