Search code examples
pythonodoo

How do I filter using the external Odoo API?


My code is as such

    search_domain = [
    '&',
    ('product_id', '=', 'Product_x'),
    '&',
    ('lot_id', '=', 'AG:04/10/2023:UID520:6666'),

    ('on_hand', '=', True),
]

# Use the search domain to find the specific location
lookuplocationbasedonname = models.execute_kw(db, uid, password, 'stock.quant', 'search_read', [search_domain])
pprint(lookuplocationbasedonname)

The search returns a stock.quant record that has the following.

  'on_hand': False,

I have tried multiple ways of filtering, the Odoo docs don't make the filtering clear.


Solution

  • Odoo sets the search attribute on the on_hand field and will use the search function when the field is used in the search domain (the field search function will be called before the model search function).

    Odoo will use all quants:

    def _search_on_hand(self, operator, value):
        """Handle the "on_hand" filter, indirectly calling `_get_domain_locations`."""
        if operator not in ['=', '!='] or not isinstance(value, bool):
            raise UserError(_('Operation not supported'))
        domain_loc = self.env['product.product']._get_domain_locations()[0]
        quant_query = self.env['stock.quant']._search(domain_loc)
        if (operator == '!=' and value is True) or (operator == '=' and value is False):
            domain_operator = 'not in'
        else:
            domain_operator = 'in'
        return [('id', domain_operator, quant_query)]
    

    The _get_domain_locations function will return all stock locations when no parameters are given

    Odoo returns 'on_hand': False because the field is not stored