英文:
Bulk Update One2Many odoo Just Change 1 data From API response
问题
我在Odoo中创建了一个API,用于在Web应用程序中使用,我的问题是当我从具有如下表单的Web应用程序中批量更新时:
从客户端端,我获得了以下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的值A和M。
这是当我打印**eval(rec['products'])**时的结果:
我只想更新'petty.cash.line'模型中的数据,需要更新的数据是:
- product_char
- quantity
- 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
and From clint side i got response JSON like this
[{"product_char": "A", "quantity": "1", "price_unit": "100"},
{"product_char": "M", "quantity": "1", "price_unit": "100"}]
and in the odoo controller this is my function :
@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):
# import pdb;pdb.set_trace()
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",
# "data": petty_cash,
}
)
i have been looping in section bulk update data like this :
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",
# "data": 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'])
i just want update data in 'petty.cash.line' 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('petty.cash.line', 'pety_cash_id', string='Pety Cash Lines', copy=True)
relation many2one 'petty.cash.line' with 'petty.cash.app' is field:
pety_cash_id = fields.Many2one('petty.cash.app', string="Pety cash")
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['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'],
})
to
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'],
})
That should get it to work because we loop update each line with its corresponding product from client.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论