Skip to Content
Menu
This question has been flagged
1 Reply
44519 Views

I need to create sale order line records based on data recieved from outside and I need to write values in those fields

in which some are relational fields and so i need to select those records from another model how is it possible to write that fields while creating a record.


example--

vals = {'order_id':sale_order,

'product_id': product_id_t,

'price_unit': price_unit_t,

'product_uom_qty': product_uom_qty_t,

'price_subtotal': price_unit_t,

'qty_delivered': product_uom_qty_t,

'state':state_t ,

'name':name_t ,

}

sale_line=self.env['sale.order.line']

sale_line.create(vals)


here product_id is a relational field corresponding to a product record and inorder to create this record what should i write to product_id OR What should i do to link that product record in sale order line record



Avatar
Discard
Best Answer

Hey Abhijith,

Its mandatory to link relation if you want to link your product etc. (many2one/many2many/one2many) to relevant model,

Here you have to check in your outside data with odoo's product (many2one) ,whether its exist or not,if not then create product and link that id (many2one) to here.same goes for one2many as well.

for editing/updating/deleting one2many and many2many please refer below syntaxes :

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

(1, ID, { values }) update the linked record with id = ID (write values on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3,ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

Avatar
Discard
Author

Thanks

Thank you so much, it was very helpful.