Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


V8 way of creating child objects only written to DB if parent is saved

Ilja Schumacher
on 3/7/16, 12:39 PM 188 views

Hi folks.

In V8 I am trying to find out how i can create child records in the Edit view of a parent object but only create the children in the database when the parent is saved.

Some pseudo code to clarify

I have the following classes:

class OrderLine:



maintenanceOrder_id= Many2One('maintenanceOrder')

class MaintenanceOrder:


parts=Many2Many( 'products')

orderLines=One2Many('orderline', 'maintenanceOrder_id')


def create_order_lines(self):

self.parts = assembly.product_ids

#this populates the view with potential Spareparts assigned to the selected assembly

Now I want to to "convert" the MaintenanceOrder.parts to MaintenanceOrder.orderlines ON THE FLY (in the view) adding a default amount of 1 and link it to the current maintenanceOrder.

The only way I see now is:

1. Getting an env in on_change

2. env.write('maintenanceOrder.orderlines'..tuple) to the db directly.

3. Link the just created Orderline to the current MaintenanceOrder

But what if the user decides to cancel creating a maintenance order mid-way?

What happens to the orderlines I just env.wrote to the db?

As far as I understood it from existing examples and modules (which are all still V7 api) people then have to handle all "Unlinks" etc. manually.

Is there a way to create and link child objects "virtually" in the on_change method? So all stuff is written to the DB only when the parent object(maintenance order) is saved.

(orderlines should not exist in the db if user clicks cancel)

My question seems pretty odd to me too.

I am propably missing somekind of "inbuilt" concept of odoo.

Would be glad if someone explained what the correct V8api-way of doing this is.

(Without manually handling creation and unlinkage of the children like in v7)

Ilja Schumacher
On 3/14/16, 1:28 PM

Got it.

As far as I understood the new logic:

In V8 one can forget about rec.write().

(New better .write() is .create() & .update() )

If you do operations in on_change() you just can ENV['MODEL'].create() records.

They will be shown in the view but not written to DB unless you save the parent record.

When it comes to creating twoway child-parent relations from the parent'onchange you just have to:

(In parent onchange)

#(virtuately, unless parent saved) Create a new child:


#add new child to existing children of parent:


#Tell the parent all its children including the newly created:


When the user is happy with what she sees jn the view now and saves it he parent and children will be written to the DB.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 3/7/16, 12:39 PM
Seen: 188 times
Last updated: 3/15/16, 4:49 AM