我們將添加一個 PUT
路由,用於更新維修訂單。
from odoo import http
from odoo.http import request
import json
class RepairOrderController(http.Controller):
# PUT 請求:更新維修訂單
@http.route('/repair/orders/<int:order_id>', type='json', auth='public', methods=['PUT'], csrf=False)
def update_repair_order(self, order_id):
try:
# 獲取 JSON 數據
data = request.jsonrequest
# 查找訂單
repair_order = request.env['repair.order'].sudo().browse(order_id)
if not repair_order.exists():
return http.Response(
json.dumps({'status': 'error', 'message': 'Order not found'}),
content_type='application/json'
)
# 更新維修訂單
repair_order.sudo().write({
'name': data.get('name', repair_order.name),
'product_id': data.get('product_id', repair_order.product_id.id),
'partner_id': data.get('partner_id', repair_order.partner_id.id),
'state': data.get('state', repair_order.state),
})
return http.Response(
json.dumps({
'status': 'success',
'message': 'Repair order updated successfully',
'order_id': order_id
}),
content_type='application/json'
)
except Exception as e:
return http.Response(
json.dumps({'status': 'error', 'message': str(e)}),
content_type='application/json'
)
PUT
http://localhost:8069/repair/orders/<order_id>
(例如 http://localhost:8069/repair/orders/1
)Content-Type: application/json
JSON
格式{
"name": "Updated Repair Order Name",
"product_id": 2,
"partner_id": 3,
"state": "confirmed"
}
PUT
請求來更新維修訂單的數據。URL
設置為 http://localhost:8069/repair/orders/<order_id>
,並將 order_id
替換為具體的維修訂單 ID。PUT
請求應返回:{
"status": "success",
"message": "Repair order updated successfully",
"order_id": 1
}