This question has been flagged
1 Reply
9848 Views

The documentation shows the search criteria of a search being a list of tuples, which are all ANDed together. Is there a way, through the API, to use more complex mixes of AND and OR. For example, given the two external IDs base.my_id_1 and l10n_uk.my_id_2, to search for both of these at the same time, I would need to search the ir.module.data model for:

model = whatever.searching.model

AND (

    (module = base AND name = my_id_1)

    OR

    (module = l10n_uk AND name = my_id_2)

)

Can that be done through the API on one call to object/search? Or would I need to do two searches - one for each module - and merge the results together?

Thanks.

Answer

Ray pointed the answer to a previous post which shows how polish notation is used. For my above example, the "(module = base AND name = my_id_1)" criteria is represented by the following elements in the criteria array (in PHP, as that's what I'm using):

"&",

["module", "=", "base"],

["name", "=", "my_id_1"]

This can be repeated as many times as needed, say N times.

Now to OR these together, the above array is prepended with N-1 "|" elements. So the full example from above, with N=2, would look like this:

[

"|",

"&",

["module", "=", "base"],

["name", "=", "my_id_1"],

"&",

["module", "=", "l10n_uk"],

["name", "=", "my_id_2"],

]

That's very easy to knock up programmatically for any value of N (just a few lines like this https://github.com/academe/openerpapi/blob/master/src/Interfaces/Object.php#L215). Additional criteria elements can be prepended (and I assume appended) to that, and they don't seem to need "&"s of their own. So the criteria does not need to be strcitly 100% polish, as the "&" operator is assumed where no operator is given, and that is how not using any "&" or "|" elements at all works anyway.

Thanks :-) Hope that's helpful to others. In all my years of coding, this is the first time I have actually had to use polish notation, and it seems fun. I would recommend anyone wanting to do this read follow your links and see how it works - my example above is not the general solution, but the application of the polish notation to my specific case, which is all data-driven (I've just plugged in the specific example values for illustration).

Avatar
Discard
Author

Thanks Ray - polish notation works a charm :-)

Best Answer

Just create the appropriate domain for your search, combining any number of AND's and OR's.

My method is explained in https://www.odoo.com/forum/help-1/question/domain-notation-using-multiple-and-nested-and-2170

 

Avatar
Discard