I know how to filter by value, but I don't know how to filter by array of values (the field contains an array).
I use Odoo 15.0 and XML-RPC to get external access to its database.
In this doc I found out how to get records filtered by a value:
XmlRpcClient client = new XmlRpcClient();
XmlRpcClientConfigImpl common_config = new XmlRpcClientConfigImpl();
String path = "https://my-domain.odoo.com";
common_config.setServerURL(new URL(String.format("%s/xmlrpc/2/common", path)));
int uid = (int) client.execute(common_config, "authenticate",
asList(db_name, login, password, emptyMap()));
final XmlRpcClient models = new XmlRpcClient() {{
setConfig(new XmlRpcClientConfigImpl() {{
setServerURL(new URL(String.format("%s/xmlrpc/2/object", path)));
}});
}};
List<Object>listProds = asList((Object[]) models.execute("execute_kw", asList(
db_name, uid, password,
"product.pricelist.item", "search_read",
asList(asList(
asList("fixed_price", "=", 0.09) // <-- filter setting
)),
new HashMap() {{
put("fields", asList(
"id", "pricelist_id", "fixed_price", "name"
));
put("limit", 300);
}}
)));
System.out.println("Read some fields for the products: " + listProds2);
For instance, using the code above I get all rows with fixed_price = 0.09
:
But I need to filter by pricelist_id
equals [2, Catalogue Price (CAD)]
, which is String representation of Object[]
. The value is an array itself, and here I'm stuck.
I tried different variants of filter syntax but none of them worked.
asList("pricelist_id", "=", asList(2, "Catalogue Price (CAD)"))
// or
asList("pricelist_id", "=", new Object[] {2, "Catalogue Price (CAD)"})
give
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: "%Catalogue Price (CAD)%" LINE 1: ...ND ("product_pricelist_item"."pricelist_id" in (2,'%Catalogu...
And the following variants of syntax have no effect as if the filter is absent:
asList("pricelist_id", "=", (new Object[]{2, "Catalogue Price (CAD)"}).toString())
asList("pricelist_id", "like", "%Catalogue Price (CAD)%")
asList("pricelist_id", "=", "2, Catalogue Price (CAD)")
asList("pricelist_id", "=", "[2, Catalogue Price (CAD)]")
It would be fine to filter by pricelist_id
containing "Catalogue Price (CAD)" (one of the array values is enough because they are unique).
By default, Odoo will return (record id, record display_name)
when reading a many2one field
To filter the result using a many2one field, you need to use the database id (2
), example:
("pricelist_id", "=", 2)
You can also filter by a list of price list ids using the in
operator:
("pricelist_id", "in", list_of_ids )