pythonodooodoo-16

Odoo - can't compare dates for setting a computed field using @api.depends


Using Odoo 16, using Odoo.sh I am trying to set a date in a model that should be the furthest in the future of the dates among the related objects from a one2many field. So I have a computed field with a @api.depends computation to iterate through the one2many and find + assign this data to a field.

Everything in my below code compiles and upgrades without problem, but I get an error when I trigger the @api.depends function by adding a order_line_ids: if order_line.delivery > record.full_delivery_date: TypeError: '>' not supported between instances of 'datetime.date' and 'bool'

Relevant code in primary model operations.purchaseorder:

full_delivery_date = fields.Date(string="Full Delivery Date (calculated)", compute='_compute_deliver_date', default=lambda self: fields.Date.today())

order_line_ids = fields.One2many('operations.purchaseorderline','purchase_order_id',string="Order Lines")

@api.depends('order_line_ids')
def _compute_deliver_date(self):
    for record in self:
        if record.order_line_ids:
            for order_line in record.order_line_ids:
                if order_line.delivery > record.full_delivery_date:
                    record.full_delivery_date = order_line.delivery

Relevant code in co-model operations.purchaseorderline:

purchase_order_id = fields.Many2one('operations.purchaseorder',string='Purchase Order')
delivery = fields.Date("Promised Delivery Date")

Solution

  • As per comment from @CZoellner the issue was because I had not put handling into the code for when order_line.delivery was empty. Correct code below:

    @api.depends('order_line_ids')
        def _compute_full_delivery_date(self):
            for record in self:
                if record.order_line_ids:
                    for order_line in record.order_line_ids:
                        if order_line.delivery:
                            if record.full_delivery_date:
                                if order_line.delivery > record.full_delivery_date:
                                    record.full_delivery_date = order_line.delivery
                            else:
                                record.full_delivery_date = order_line.delivery