如何使用 Odoo API(XMLRPC) 设置具有 one 2many、many2one 和 many2many 字段的相关字段?

How to set a related field with a one 2many, many2one and many2many field, using Odoo API(XMLRPC)?

Odoo 新手,我正在尝试在 Java 中创建 Web 服务,将数据加载到 Odoo,特别是模型 product.template 和 technical.sheet,一个自定义我创建的模型。对于一种产品,可以有 0 个或多个技术 sheet,对于一种技术 sheet - 一种产品。 在模型 technical.sheet 中,我创建了一个字段 x_product_id,类型 - many2one 和对象关系 - product.tamplate。在 product.template 中,我创建了相同的字段 x_product_id,但类型是 one2many,对象关系 - technical.sheet 和字段关系 x_product_id(technical.sheet)。 我已成功加载产品和技术 sheet,但是当我尝试设置关系字段时,出现错误。 是否有人有示例或想法如何使用 Odoo 外部 API 设置 one2many、many2one 和 many2many 类型的字段? 谢谢! 这是一段代码:

for(文章文章: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 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());
                                                           }})
                         ));

                  }

          }

要使用相应的值(记录)填充或操作 one2many 或 many2many 字段,您需要使用 special commands

在下面的例子中我们添加了一个新的订单行(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);
                                    }}
                                 )
                             )
                        ); 
                }}
            )
        ));

此代码有效。我创建了文章列表 (product.template),每篇文章都与零个或多个技术表(我在 odoo 上创建的自定义模型)相关,关系 - one2many。

HashMap<String, Article> articles = (HashMap<String, Article>)globalMap.get("articles");
          
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());
          }