【问题标题】:How to insert one2many values in odoo using xml-rpc如何使用 xml-rpc 在 odoo 中插入 one2many 值
【发布时间】:2016-04-05 07:46:55
【问题描述】:

目前我正在使用 odoo 8.0。实际上,我正在使用 XML-RPC API 创建产品。这里是使用 php 从 xml-rpc 创建产品的代码。

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

产品已成功创建。然后我手动创建属性名称 Color (attribute_id = 1) 和值 green (value_id = 1)。接下来我将通过下面的代码来更新上面的 varint(Color)。

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

没有错误。它打印更新的 id。但是这些变体不会在 odoo 前端的产品表单视图中更新。 'attribute_line_ids' 是 product.template 对象中的 one2many 字段。我认为从 xml-rpc php 更新 one2many 字段的语法不正确。请帮我。提前致谢。

【问题讨论】:

    标签: php xml-rpc odoo-8 openerp-8


    【解决方案1】:

    One2many 模型始终持有foreign key 或我说的Many2one 它是关联模型。

    例如:

    在 ODOO 中,product.templateproduct.attribute.line 使用字段 attribute_line_ids 具有 One2many 关系。

    并且product.attribute.lineproduct.template 使用字段product_tmpl_id 具有Many2one 关系。

    如果要在 attribute_line_ids 中插入值,则必须在 product.attribute.line 中创建记录。

    请通过这段代码sn-p:

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

    我很确定它会帮助您解决问题。

    【讨论】:

      【解决方案2】:

      Sharma 的回答看起来是正确的(虽然我没有使用 PHP),但需要更多解释:编写 2many 字段使用包含“命令”代码的三元组。 Sharma 回答中的神奇“6”是使用“替换”命令。

      (0,_ ,{' field': value}): This creates a new record and links it to this one
      (1, id,{' field': value}): This updates values on an already linked record
      (2, id,_): This unlinks and deletes a related record
      (3, id,_): This unlinks but does not delete a related record
      (4, id,_): This links an already existing record
      (5,_,_): This unlinks but does not delete all linked records
      (6,_,[ ids]): This replaces the list of linked records with the provided list
      
      The underscore symbol used above represents irrelevant values, usually filled with 0 or False.
      

      雷斯,丹尼尔。 Odoo Development Essentials Packt 出版,2015 年

      PS - 我的印刷版中的第 65 页,索引在我拥有的版本中很远。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-18
        • 1970-01-01
        • 2019-05-03
        • 2021-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多