how to overwrite a one to many records in odoo through default API

只谈情不闲聊 提交于 2019-12-11 15:18:41

问题


how to overwrite a one to many records in odoo through odoo API ?

This is my create json, what change I want to make in this json to overwrite(Replace) the existing? lead_product_ids., now it is appending the records. Now i am getting multiple when update the records in this code instead of 0,0 what is the value, Please help.

{
  "jsonrpc": "2.0",
  "params": {
    "model": "crm.lead",
    "method": "create",
    "args": [
      {
        "type": "opportunity",
        "name": "Fgtrdhjkkmmmmmmmm1290",
        "pro_info": "Fggggggg hhhhhh jkkkkkkknjj hjkll",
        "tag_ids": [

           6,
              0,
              [
                43,42
              ]
        ],
        "purposes_id": 3,
        "lead_product_ids": [
          ***0,
          0,***
          {
            "product_uom": 21,
            "product_id": 148,
            "description": "",
            "qty": 1,
            "price_unit": 2448,
            "expected_price": 2448,
            "discount": 0,
            "tax_id": [
              6,
              0,
              [
                22
              ]
            ],
            "price_subtotal": 2741.760009765625
          }
        ],
        "partner_id": 1592,
        "religion": 2,
        "age_bucket": "40_45",
        "phone": "5695324877",
        "mobile": "5695324878",
        "locations_id": 157,
        "district_id": 157,
        "state_id": 593
      }
    ]
  }
}

回答1:


The answer is found in the docstring of the Model.write():

    """
      ...
      This format is a list of triplets executed sequentially, where each
      triplet is a command to execute on the set of records. Not all
      commands apply in all situations. Possible commands are:

      ``(0, _, values)``
          adds a new record created from the provided ``value`` dict.
      ``(1, id, values)``
          updates an existing record of id ``id`` with the values in
          ``values``. Can not be used in :meth:`~.create`.
      ``(2, id, _)``
          removes the record of id ``id`` from the set, then deletes it
          (from the database). Can not be used in :meth:`~.create`.
      ``(3, id, _)``
          removes the record of id ``id`` from the set, but does not
          delete it. Can not be used on
          :class:`~odoo.fields.One2many`. Can not be used in
          :meth:`~.create`.
      ``(4, id, _)``
          adds an existing record of id ``id`` to the set. Can not be
          used on :class:`~odoo.fields.One2many`.
      ``(5, _, _)``
          removes all records from the set, equivalent to using the
          command ``3`` on every record explicitly. Can not be used on
          :class:`~odoo.fields.One2many`. Can not be used in
          :meth:`~.create`.
      ``(6, _, ids)``
          replaces all existing records in the set by the ``ids`` list,
          equivalent to using the command ``5`` followed by a command
          ``4`` for each ``id`` in ``ids``.

      .. note:: Values marked as ``_`` in the list above are ignored and
                can be anything, generally ``0`` or ``False``.
    """

It's (1, id, {'field_1': value_1,'field_2': value_2,}). But you should use write instead of create because in create it doesn't make any sense to change non-existing records of a x2many field.



来源:https://stackoverflow.com/questions/55006401/how-to-overwrite-a-one-to-many-records-in-odoo-through-default-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!