Search code examples
pythonodoo

Comparing value from a foreign key in Odoo


I need to set the value of a boolean to True if the value of any order_line is 11, however it does not work if I set the line using "l.route_id == 11", if I set the line to "lambda l: l.route_id" it works but it could have other number different than 11. route_id will be Null usually.

The database format of route_id is int4

def _compute_dropship_notes_ind(self):
        for order in self:
            # Check if any line has dropshipping activated
            any_dropship_lines = order.order_line.filtered(lambda l: l.route_id )
            #any_dropship_lines = order.order_line.filtered(lambda l: l.route_id == 11)
            order.dropship_notes_ind = bool(any_dropship_lines)

If I use this line --> any_dropship_lines = order.order_line.filtered(lambda l: l.route_id == 11) order.dropship_notes_ind will be False

But if I use any_dropship_lines = order.order_line.filtered(lambda l: l.route_id) order.dropship_notes_ind will be True

The values in the database for that column and that order are 11 and Null, a mix of both of them. route_id is a foreign key also.


Solution

  • If I'm not mistaken, route_id is a many2one, so you need to add .id.

    Basically, you want to get the order_line if the route has the id 11.

    any_dropship_lines = order.order_line.filtered(lambda l: l.route_id.id == 11)