【问题标题】:How to set a related field with a one 2many, many2one and many2many field, using Odoo API(XMLRPC)?如何使用 Odoo API(XMLRPC) 使用一个 2many、many2one 和 many2many 字段设置相关字段?
【发布时间】:2020-04-16 18:04:11
【问题描述】:

Odoo 的新手,我正在尝试用 Java 创建一个 Web 服务,将数据加载到 Odoo 中,特别是加载到模型 product.template 和 Technical.sheet 中,这是我创建的自定义模型。对于一种产品,可以有 0 个或许多技术表,而对于一个技术表 - 一种产品。 在模型 Technical.sheet 中,我创建了一个字段 x_product_id、类型 - many2one 和对象关系 - product.tamplate。在 product.template 中,我创建了相同的字段 x_product_id,但类型是 one2many,对象关系 - Technical.sheet 和字段关系 x_product_id(technical.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());
                                                           }})
                         ));

                  }

          }

【问题讨论】:

  • 嗨,欢迎来到 SO,你能通过很好地格式化帖子顶部来更新你的帖子吗?没有段落没有换行符,这不太可能有人会完整地阅读它

标签: api web-services odoo xml-rpc


【解决方案1】:

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

在以下示例中,我们添加一个新订单行 (order_lineOne2many):

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);
                                    }}
                                 )
                             )
                        ); 
                }}
            )
        ));

【讨论】:

    【解决方案2】:

    此代码有效。我创建了文章列表 (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());
              }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多