批量更新Odoo中的One2Many字段,只需从API响应中更改1个数据。

huangapple go评论58阅读模式
英文:

Bulk Update One2Many odoo Just Change 1 data From API response

问题

我在Odoo中创建了一个API,用于在Web应用程序中使用,我的问题是当我从具有如下表单的Web应用程序中批量更新时:

批量更新Odoo中的One2Many字段,只需从API响应中更改1个数据。

从客户端端,我获得了以下JSON响应:

[
    {"product_char": "A", "quantity": "1", "price_unit": "100"},
    {"product_char": "M", "quantity": "1", "price_unit": "100"}
]

在Odoo控制器中,这是我的函数:

@http.route([
    '/api/update/petty_cash/<int:id>'
], type='http', auth="none", methods=['POST'], csrf=False)
@check_valid_token
def update_petty_cash(self, id, **rec):
    user_id = request.uid
    petty_cash = request.env['petty.cash.app'].\
        search([('create_uid', '=', user_id),
                ('id', '=', id)
                ])

    image_file = rec.get("image", False)
    replace = image_file.replace(" ", "+")
    user_id = request.uid
    petty_cash.write({
        'reference': rec['reference'],
        'partner_id': rec['partner_id'],
        'state': petty_cash.state,
        'request_by': user_id,
        'date_requested': fields.Date.today(),
        'image': replace,
    })

    petty_cash_line = request.env['petty.cash.line'].sudo().search([('pety_cash_id', '=', petty_cash.id)])
    for product in eval(rec['products']):
        petty_cash_line.update({
            'pety_cash_id': petty_cash.id,
            'product_char': product['product_char'],
            'quantity': product['quantity'],
            'price_unit': product['price_unit'],
        })

    return valid_response(
        200, {
            "msg" : "Data Updated",
        }
    )

但是数据库中的product_char字段只写入了一个数据,只有M的值发生了变化,尽管我从客户端端获取了两个product_char的值AM

这是当我打印**eval(rec['products'])**时的结果:

批量更新Odoo中的One2Many字段,只需从API响应中更改1个数据。

我只想更新'petty.cash.line'模型中的数据,需要更新的数据是:

  1. product_char
  2. quantity
  3. price_unit

关于'petty.cash.app'与'petty.cash.line'之间的one2Many关系,字段如下:

pety_cash_line_ids = fields.One2many('petty.cash.line', 'pety_cash_id', string='Pety Cash Lines', copy=True)

关于'petty.cash.line'与'petty.cash.app'之间的many2one关系,字段如下:

pety_cash_id = fields.Many2one('petty.cash.app', string="Pety cash")

我已经将Odoo中的update方法更改为write方法,但结果仍然相同,只写入了一个数据。我的代码有什么问题吗?请帮助,谢谢。

英文:

i create API with Odoo for consume in web apps, my problem it's when i bulk update from web apps which has form like this

批量更新Odoo中的One2Many字段,只需从API响应中更改1个数据。

and From clint side i got response JSON like this

[{&quot;product_char&quot;: &quot;A&quot;, &quot;quantity&quot;: &quot;1&quot;, &quot;price_unit&quot;: &quot;100&quot;},
{&quot;product_char&quot;: &quot;M&quot;, &quot;quantity&quot;: &quot;1&quot;, &quot;price_unit&quot;: &quot;100&quot;}]

and in the odoo controller this is my function :

@http.route([
    &#39;/api/update/petty_cash/&lt;int:id&gt;&#39;
], type=&#39;http&#39;, auth=&quot;none&quot;, methods=[&#39;POST&#39;], csrf=False)
@check_valid_token
def update_petty_cash(self, id, **rec):
    # import pdb;pdb.set_trace()
    user_id = request.uid
    petty_cash = request.env[&#39;petty.cash.app&#39;].\
        search([(&#39;create_uid&#39;, &#39;=&#39;, user_id),
                (&#39;id&#39;, &#39;=&#39;, id)
                ])

    image_file = rec.get(&quot;image&quot;, False)
    replace = image_file.replace(&quot; &quot;, &quot;+&quot;)
    user_id = request.uid
    petty_cash.write({
        &#39;reference&#39;: rec[&#39;reference&#39;],
        &#39;partner_id&#39;: rec[&#39;partner_id&#39;],
        &#39;state&#39;: petty_cash.state,
        &#39;request_by&#39;: user_id,
        &#39;date_requested&#39;: fields.Date.today(),
        &#39;image&#39;: replace,

    })

    petty_cash_line = request.env[&#39;petty.cash.line&#39;].sudo().search([(&#39;pety_cash_id&#39;, &#39;=&#39;, petty_cash.id)])
    for product in eval(rec[&#39;products&#39;]):
        petty_cash_line.update({
            &#39;pety_cash_id&#39;: petty_cash.id,
            &#39;product_char&#39;: product[&#39;product_char&#39;],
            &#39;quantity&#39;: product[&#39;quantity&#39;],
            &#39;price_unit&#39;: product[&#39;price_unit&#39;],
        })

    return valid_response(
        200, {
            &quot;msg&quot; : &quot;Data Updated&quot;,
            # &quot;data&quot;: petty_cash,
        }
    )

i have been looping in section bulk update data like this :

petty_cash_line = request.env[&#39;petty.cash.line&#39;].sudo().search([(&#39;pety_cash_id&#39;, &#39;=&#39;, petty_cash.id)])
    for product in eval(rec[&#39;products&#39;]):
        petty_cash_line.update({
            &#39;pety_cash_id&#39;: petty_cash.id,
            &#39;product_char&#39;: product[&#39;product_char&#39;],
            &#39;quantity&#39;: product[&#39;quantity&#39;],
            &#39;price_unit&#39;: product[&#39;price_unit&#39;],
        })

    return valid_response(
        200, {
            &quot;msg&quot; : &quot;Data Updated&quot;,
            # &quot;data&quot;: petty_cash,
        }
    )

but data product_char in database just write 1 data, just value M changed data, whereas i get 2 value product_char from client side A and M,

this is result when i print eval(rec['products'])

批量更新Odoo中的One2Many字段,只需从API响应中更改1个数据。

i just want update data in &#39;petty.cash.line&#39; model , data needs to update is :
1. product_char
2. quantity
3. price_unit

relation one2Many 'petty.cash.app' with 'petty.cash.line' is field:

 pety_cash_line_ids = fields.One2many(&#39;petty.cash.line&#39;, &#39;pety_cash_id&#39;, string=&#39;Pety Cash Lines&#39;, copy=True)

relation many2one 'petty.cash.line' with 'petty.cash.app' is field:

 pety_cash_id = fields.Many2one(&#39;petty.cash.app&#39;, string=&quot;Pety cash&quot;)

i have been change update method, to write method in odoo, but result is same, just write 1 data, whats wrong from my code ? please help

Thanks

答案1

得分: 1

将这段代码更改为:

petty_cash_line = request.env['petty.cash.line'].sudo().search([('pety_cash_id', '=', petty_cash.id)])
products = eval(rec['products'])
for index, line in enumerate(petty_cash_line):
    line.update({
        'product_char': products[index]['product_char'],
        'quantity': products[index]['quantity'],
        'price_unit': products[index]['price_unit'],
    })

这样应该可以使其正常工作,因为我们循环更新每行,使用与客户端中相应产品相对应的值。

英文:

Change this code

petty_cash_line = request.env[&#39;petty.cash.line&#39;].sudo().search([(&#39;pety_cash_id&#39;, &#39;=&#39;, petty_cash.id)])
    for product in eval(rec[&#39;products&#39;]):
        petty_cash_line.update({
            &#39;pety_cash_id&#39;: petty_cash.id,
            &#39;product_char&#39;: product[&#39;product_char&#39;],
            &#39;quantity&#39;: product[&#39;quantity&#39;],
            &#39;price_unit&#39;: product[&#39;price_unit&#39;],
        })

to

petty_cash_line = request.env[&#39;petty.cash.line&#39;].sudo().search([(&#39;pety_cash_id&#39;, &#39;=&#39;, petty_cash.id)])
products = eval(rec[&#39;products&#39;])
for index, line in enumerate(petty_cash_line):
  line.update({
    &#39;product_char&#39;: products[index][&#39;product_char&#39;],
    &#39;quantity&#39;: products[index][&#39;quantity&#39;],
    &#39;price_unit&#39;: products[index][&#39;price_unit&#39;],
})

That should get it to work because we loop update each line with its corresponding product from client.

huangapple
  • 本文由 发表于 2023年3月8日 15:22:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定