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
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.