简体   繁体   中英

Odoo web service API to create a sales order record with 3 items

I have been trying to create a sales order record with 3 items, but have not been able to. I can however create a record with 1 item successfully. Not sure how to add multiple items, tried various cases following code helped me create a record with 1 item

line_vals = {
    'product_id': product2_ids[0].get('id'),
    'name':'test',
    'product_uom_qty': 10,
    'price_unit': 30000,
}

order_vals = {
    'partner_id': customer_ids[0].get('id'),
    'validity_date': datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'),
    'order_line': [(0, 0, line_vals)],
}

salesorder = objects.execute_kw(db, uid, pwd, 'sale.order', 'create', [order_vals])

tried changing this code for many items, it could only create one or failed. Any suggestions?

Generally you can manage One2many fields by two way, either you can write many2one reference in inverse model so framework will manage one2many automatically or you can prepare list of tuples and directly write one2many fields with it's parent record.

Let's take an example of Account invoice and invoice lines. So if you want to create invoice lines then you have two possible options either you first create account.invoice record and then give that invoice reference in all invoice lines at the time of preparing data or other way is to prepare account invoice data and in that also prepare invoice lines data (list of tuple) and pass it to account.invoice .

Here the second way is used.

(0, 0, { values }) : link to a new record that needs to be created with the given values dictionary.

If order has multiple order lines then you can create list of tuple, like [(0, 0,{values}),(0, 0,{values}),(0, 0,{values})] .

You should follow below code to create multiple lines in same order.

order_lines=[]

for product in products:
    line_vals = {
        'product_id': product.id,
        'name':'product.name,
        'product_uom_qty': product.product_uom_qty,
        'price_unit': product.price_unit,
    }
    order_lines.append((0, 0,line_vals))

order_vals = {
    'partner_id': customer_ids[0].get('id'),
    'validity_date': datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'),
    'order_line':order_lines,
}

salesorder = objects.execute_kw(db, uid, pwd, 'sale.order', 'create', [order_vals])

This may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM