Skip to Content
Menu
This question has been flagged
1 Reply
2737 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:

part=Many2One('product')

amount=Integer()

maintenanceOrder_id= Many2One('maintenanceOrder')


class MaintenanceOrder:

assembly=Many2One('assembly')

parts=Many2Many( 'products')

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


@api.on_change(assembly)

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)

Avatar
Discard
Author Best Answer

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:

child=ENV['CHILDMODEL'].create({'PARENT_ID':self.ID})

#add new child to existing children of parent:

CHILDREN=self.CHILD_IDS.append(child)

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

self.update({'CHILD_IDS':CHILDREN})

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.

Avatar
Discard
Related Posts Replies Views Activity
0
Jan 24
379
2
May 23
6771
1
Feb 22
21416
5
Dec 21
15005
0
Dec 20
3653