Odoo的新手,我必须使用Odoo API的读取方法来获取与产品相关的类别。我得到一个对象列表,然后对于每个类别,我都需要提取字段product_tmpl_ids和一个整数列表。我不确定该怎么做。这是方法:
public List readModelfields(String modelName, List<Integer>ids, List<String> fields, Integer uid)throws XmlRpcException {
List record = (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
db, uid, password,
modelName, "read",
ids,
new HashMap() {{
put("fields", fields);
}}
)));
return record;
}
这是其余代码:
List<String> fields = new ArrayList<>();
fields.add("product_tmpl_ids");
List categoryIds = (List<Integer>)service.searchByStrParameter("product.public.category", "name", "A - Administration / Office", uid);
List result = (List)service.readModelfields("product.public.category", categoryIds, fields, uid);
有人可以帮忙吗?如何从获取的对象中提取字段?
您已将搜索方法(列表)的结果传递给readModelfields
,该属性按原样调用read
方法,如外部API read方法ids
中所述,必须在列表中传递:
Arrays.asList(ids),
您使用搜索,然后在同一型号上进行读取,而是使用search_read以下示例显示如何获取'res.partners'的many2many字段category_id
public List searchReadModelfields(String modelName, List domain, List<String> fields, Integer uid) throws XmlRpcException {
return (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
db, uid, password,
modelName,
"search_read",
Arrays.asList(domain),
new HashMap() {{
put("fields", fields);
}}
)));
}
调用searchReadModelfields
并打印出值:
List<String> fields = new ArrayList<>();
fields.add("category_id");
List domain = Arrays.asList(
Arrays.asList("category_id", "!=", false)
);
// return list of hashmaps)
List result = (List)service.searchReadModelfields("res.partner", domain, fields, uid);
HashMap values;
Integer partner_id;
Object[] category_ids;
for(Object partner_data: result) {
values = (HashMap)partner_data;
partner_id = (Integer) values.get("id");
category_ids = (Object [])values.get("category_id");
System.out.printf("{partner_id: %s, ", partner_id);
System.out.print("category_ids:[");
for(Object category_id: category_ids) {
System.out.printf("%s,",
category_id
);
}
System.out.println("]}");
}