Nuevo en Odoo, estoy tratando de crear un servicio web en Java, cargando datos en Odoo, en particular, en los modelos product.template y technical.sheet, un modelo personalizado que creé. Para un producto puede haber 0 o muchas hojas técnicas y para una hoja técnica: un producto. En el modelo technical.sheet creé un campo x_product_id, el tipo - many2one y la relación de objeto - product.tamplate. En product.template creo el mismo campo, x_product_id, pero el tipo es one2many, la relación de objeto - technical.sheet y la relación de campo x_product_id (technical.sheet). Logré cargar los productos y las hojas técnicas, pero cuando intento configurar los campos relacionales, obtengo errores. ¿Alguien tiene un ejemplo o una idea de cómo configurar campos de tipo one2many, many2one y many2many, utilizando la API externa de Odoo? ¡Gracias! Esta es la pieza de código:
Para (Artículo artículo: artículos.valores ()) {
if(article.getTechSheets().size()>0){
technicalSheetsMap.put(article.getMfsIdentifier(), article.getTechSheets());
}
ArrayList<Integer> techSheetsIds = new ArrayList<>();
for(TechnicalSheet sh: article.getTechSheets()){
techSheetsIds.add(Integer.valueOf(sh.getId()));
}
ArrayList<Integer> arrids = new ArrayList<Integer>();
arrids.add(Integer.valueOf(article.getMfsIdentifier()));
/* Load of the article into model product.tamplate of the odoo database */
final Integer id = (Integer)models.execute("execute_kw", Arrays.asList(
db, uid, password,"product.template", "create",
Arrays.asList(new HashMap() {{
put("is_published", true);
put("active", true);
put("x_standartization_level", article.getStandartizationLevel());
put("id", Integer.valueOf(article.getMfsIdentifier()));
put("default_code", article.getCode());
put("name", article.getLabelEn());
put("x_msf_identifier", article.getMfsIdentifier());
put("display_name", article.getLabelEn());
put("x_label_en", article.getLabelEn());
put("x_oca", article.isOca());
put("x_ocb", article.isOcb());
put("x_ocba", article.isOcba());
put("x_ocg", article.isOcg());
put("x_ocp", article.isOcp());
put("x_cold_chain_group", article.getTermosensitive());
put("x_justification_id", article.getJustificationId());
put("x_transport_un_code_id", article.getTransportUnCodeId());
put("x_picture_content", article.getPictureNb());
put("x_picture_label", article.getPictureLabel());
put("x_controlled_substance", article.getControlledSubstance());
put("x_medical_device_class", article.getMedicalDeviceClass());
put("x_code", article.getCode());
put("x_type", article.getType());
put("x_family_id", article.getFamilyId());
put("x_group_id", article.getGroupId());
put("x_who_id", article.getWhoIds());
put("x_product_id", Integer.valueOf(article.getMfsIdentifier()));
}})
));
for(TechnicalSheet sheet: article.getTechSheets()){
final Integer idsh = (Integer)models.execute("execute_kw", Arrays.asList(
db, uid, password,
"x_product.technical_sheet",
"create",
Arrays.asList(new HashMap(){{
put("id", sheet.getId());
put("x_name", sheet.getLabelEn());
put("x_description", sheet.getDefinition());
put("display_name", sheet.getLabelEn());
put("x_product_id", Integer.valueOf(article.getMfsIdentifier()));
put("x_norms", sheet.getNorms());
put("x_precat", sheet.getPrecat());
}})
));
}
}
2 respuestas
Para rellenar o manipular one2many o many2many field con los valores (registros) correspondientes, debe usar comandos especiales.
En el siguiente ejemplo, agregamos una nueva línea de pedido (order_line
One2many):
main.models.execute("execute_kw", Arrays.asList(
main.db, uid, main.password,
"sale.order", "write",
Arrays.asList(
Arrays.asList(20),
new HashMap() {{
put("order_line",
Arrays.asList(
Arrays.asList(0, 0,
new HashMap() {{
put("product_id", 3);
}}
)
)
);
}}
)
));
Este código funcionó. Creé una lista de artículos (product.template), cada artículo está relacionado con cero o muchas hojas técnicas (un modelo personalizado que creé en odoo), relación - one2many.
Artículos HashMap = (HashMap) globalMap.get ("artículos");
String username = "****";
String password = "****";
String db = "****";
String url ="****";
try{
final XmlRpcClient authClient = new XmlRpcClient();
final XmlRpcClientConfigImpl authStartConfig = new XmlRpcClientConfigImpl();
authStartConfig.setServerURL(new URL(String.format("%s/xmlrpc/2/common", url)));
List configList = new ArrayList();
Map paramMap = new HashMap();
configList.add(db);
configList.add(username);
configList.add(password);
configList.add(paramMap);
int uid = (int)authClient.execute(authStartConfig, "authenticate", configList);
final XmlRpcClient models = new XmlRpcClient() {{
setConfig(new XmlRpcClientConfigImpl() {{
setServerURL(new URL(String.format("%s/xmlrpc/2/object", url)));
}});
}};
/* Loop all the articles */
for(Article article: articles.values()) {
if(article.getTechSheets().size()>0){
technicalSheetsMap.put(article.getMfsIdentifier(), article.getTechSheets());
}
ArrayList<Integer> techSheetsIds = new ArrayList<>();
for(TechnicalSheet sh: article.getTechSheets()){
techSheetsIds.add(Integer.valueOf(sh.getId()));
}
ArrayList<Integer> arrids = new ArrayList<Integer>();
arrids.add(Integer.valueOf(article.getMfsIdentifier()));
/* Load of the articles into model product.template of the odoo database */
final Integer id = (Integer)models.execute("execute_kw", Arrays.asList(
db, uid, password,
"product.template", "create",
Arrays.asList(new HashMap() {{
put("is_published", true);
put("active", true);
put("x_standartization_level", article.getStandartizationLevel());
put("id", Integer.valueOf(article.getMfsIdentifier()));
put("default_code", article.getCode());
put("name", article.getLabelEn());
put("x_msf_identifier", article.getMfsIdentifier());
put("display_name", article.getLabelEn());
put("x_label_en", article.getLabelEn());
put("x_oca", article.isOca());
put("x_ocb", article.isOcb());
put("x_ocba", article.isOcba());
put("x_ocg", article.isOcg());
put("x_ocp", article.isOcp());
put("x_cold_chain_group", article.getTermosensitive());
put("x_justification_id", article.getJustificationId());
put("x_transport_un_code_id", article.getTransportUnCodeId());
put("x_picture_content", article.getPictureNb());
put("x_picture_label", article.getPictureLabel());
put("x_controlled_substance", article.getControlledSubstance());
put("x_medical_device_class", article.getMedicalDeviceClass());
put("x_code", article.getCode());
put("x_type", article.getType());
put("x_family_id", article.getFamilyId());
put("x_group_id", article.getGroupId());
put("x_who_id", article.getWhoIds());
/* Set Technical Sheets*/
if(article.getTechSheets().size()>0){
put("x_product_id",
Arrays.asList(
Arrays.asList(
0,
0,
new HashMap(){{
for(TechnicalSheet sheet: article.getTechSheets()){
put("id", sheet.getId());
put("x_name", sheet.getLabelEn());
put("x_description", sheet.getDefinition());
put("display_name", sheet.getLabelEn());
put("x_product_id", article.getMfsIdentifier());
put("x_norms", sheet.getNorms());
put("x_precat", sheet.getPrecat());
}
}}
)));
}}})
));
}
}catch(MalformedURLException e){
System.out.print("MalformedURLException: "+e.toString()+" "+e.getStackTrace());
}
catch(XmlRpcException e){
System.out.print("XmlRpcException: "+e.toString()+" "+e.getStackTrace());
}
Nuevas preguntas
api
NO UTILICE. Utilice etiquetas específicas como [google-cloud-platform], [facebook], [amazon-web-services] en su lugar o [api-design] cuando corresponda. Las preguntas que piden recomendar o encontrar una API están fuera de tema.