douci2516 2016-04-05 07:46
浏览 308
已采纳

如何使用xml-rpc在odoo中插入one2many值

Currently I am using odoo 8.0. Actually I am creating the product using the XML-RPC API. Here the code for creating the product from xml-rpc using php.

$url = "http://localhost:8069";
$db = "xmlcreate";
$username = "admin";
$password = "admin";
require_once('ripcord-master/ripcord.php');
$common = ripcord::client("$url/xmlrpc/2/common");
$uid = $common->authenticate($db, $username, $password, array());
$models = ripcord::client("$url/xmlrpc/2/object");

$product = array('name' => 'Sample',
                 'type' => 'product',
                 'list_price' => 4.6,
                 'standard_price' => 3.25
           );
$product_id = $models->execute_kw($db, $uid, $password,  'product.template','create',array($product));

The product was created successfully. Then I manually create the attribute name Color (attribute_id = 1) and the value green (value_id = 1). Next I am going to update the above varaint(Color) by the following code.

$attributes = array();
$attributes[] = 0;
$attributes[] = 0;
$attributes['attribute_id'] = 1; // attribute is color (color -> 1)
$attributes['values_id'] = array(1); // attribute value is green(green -> 1) 

$existing_prodid = 1;
$up_attr_id = $models->execute_kw($db, $uid, $password,'product.template','write',array($existing_prodid, array('attribute_line_ids' => $attributes)));
print_r($up_attr_id);

There is no error. It prints the updated id. But the variants are not updated in the products form view in odoo frontend. The 'attribute_line_ids' is one2many fields in product.template object. I think the syntax is incorrect for updating one2many fields from xml-rpc php. please help me. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongmei3498 2016-04-17 11:18
    关注

    One2many model always hold the foreign key or i say Many2one of it's associative model .

    For example:

    In ODOO the product.template have One2many relation with product.attribute.line using field attribute_line_ids .

    And product.attribute.line have Many2one relation with product.template using field product_tmpl_id.

    If you want to insert a value in attribute_line_ids , then you will have to create a record in product.attribute.line.

    Please go through this code snippet :

    $existing_prodid = 59;
    $existing_attribute_id = 2;
    $existing_value_id = 4;
    $product_attribute_line = $models->execute($db, $uid, $password,
                                           'product.attribute.line','create',
                                            array('product_tmpl_id' => $existing_prodid;,
                                                'attribute_id'=>$existing_attribute_id,
                                                'value_ids'=>array(array(6,0,array($existing_value_id)))
                                                     ))
    

    I am damn sure that it's will help you in solving your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改