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条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛