Odoo Help

1

[Bug] O2M lines populated from Onchange Event, gets appended rather than overriding, when saving....

By
deep
on 9/15/14, 6:27 AM 1,565 views

Using Onchange Event am trying to populate values to One2Many field.. (which is working fine)

But, Consider a scenario... 

Once the record is saved, And again I try to change the value (event triggering field), which in turn calls the Onchange Event, and it again populates the fresh  new One2Many lists... (which is also working perfectly)

But when I save the record, then list  values are changed... i.e. instead of overriding the O2M values, with new lists, it actually appends to the existing list.,....

Let me explain with acute scenario...

I have an object, based on Sale Order Id, am trying to capture its Order lines.... So all i have done is... I have added Order_id (M2O) and Order_lines(O2M)... Onchange Event is written for Order_id, which returns Order_lines pertaining to its order.... (which is obsolutely working fine)...

But the problem arises, with the saved records, If I change the Order_id, surely onchange fetches me the new lines pertaining to the new Order_id, but when I save the record... Order_lines are changed... (i.e. lines of pervious Order and lines of new Order gets saved)...

So I believe, this is a bug in Openerp, because am pretty sure there is nothing wrong in my code... Anybody faced this kind of problem, do let me know....

can u post your code?..

Prakash
on 9/15/14, 6:30 AM

In xml file field set context and in python file onchange using context.get('') check particular field is called then return o2m lines populated values otherwise return {}

Prakash
on 9/15/14, 6:48 AM

Hmmmm... I think you didnt get my question.... let me explain with other example... consider field A and field B... Now when field A is changed say XXX, then value of field B is also changed right say YYYY.... again if you change the field A to PPPP, then certainly field B value will also be overriden with new value QQQ...

deep
on 9/15/14, 6:57 AM

Same thing wont happen when field B happened to be an One2many field... have you ever come across such scenario ?

deep
on 9/15/14, 6:59 AM

similar issue i faced more than one many2one field onchange applied. Using context fixed the issues.

Prakash
on 9/15/14, 7:16 AM

Thanks.. I tried context, even that didn't help me...

deep
on 9/17/14, 1:39 AM
1

Hi Deep,

You have to just take care and learn from the following thing.

# (0, 0,  { fields })    create
# (1, ID, { fields })    update (write fields to ID)
# (2, ID)                remove (calls unlink on ID, that will also delete the relationship because of the ondelete)
# (3, ID)                unlink (delete the relationship between the two objects but does not delete ID)
# (4, ID)                link (add a relationship)
# (5, ID)                unlink all
# (6, 0, ids)            set a list of link

Here you have to use the 'one2many_fieldname':[6,0,[(), (), (), ...] ] into return value. It will never append records, It will always create new record.

Let me knwo if it is not clear to you.

 

Hi Chirag... what you are telling is all about Many2Many... but what i need is w.r.t One2many... and I do know these things... but thanks anyway....

deep
on 9/15/14, 9:52 AM

Only one thing chirag forgot to mention... and it is the same for m2m and o2m fields... if you fill it with onchange action... do not forget to empty old vals before filling it... be careful with that part, because you do not want to unlink actual data.. just break the relationship for that field... so the simplest way is to call unlink for all record curently related, and then fill them again...

Bole
on 9/16/14, 10:15 AM

Hi Bole, yes presently i have done tat only, unlinking current records.... but i dont understand.. how can u use [6,0... for One2Many.. would you to care to share the code.... thanks

deep
on 9/16/14, 10:35 AM

what chirag wrote is for many2many... fro one2many you can only use this: (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) so you will probably need to do (2,id) for id in ids... then add new records with 0 or 1...

Bole
on 9/16/14, 12:12 PM
0
deep
On 9/17/14, 1:38 AM

Guys... I really appreciate the efforts you took to help me out... but this is not the solution am looking for..... And am perfectly well aware of the OneMany & Many2Many semantics, and indeed those things have served my other requirements efficiently...

But what am looking for is not this, I guess it is a bug in openerp....

Thanks anyway...

Yes, I have similar problem with you. In my case I also get mysterious first entry in lines (I'm investigating bank.statement.lines) which should be not there. I let the append bug through and call it my use case (unless someone ask me to do proper update, sigh). I think you can submit proper bug report in the github repo.

Ben Bernard
on 9/24/14, 4:12 AM

Forget to mention, don't add string "bug" in the title or your post will be closed by the founder itself, usually. Just add a proper tag ;)

Ben Bernard
on 9/24/14, 4:19 AM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 9/15/14, 6:27 AM
Seen: 1565 times
Last updated: 3/16/15, 8:10 AM