Skip to Content
Menu
This question has been flagged
2 Replies
2788 Zobrazenia

hi all,

i created a One2many relation like this

line_ids = fields.One2many('comodel', 'relation_id', string = "something")


in the past e.g. odoo 10 i could add a value from code in the One2many field by doing:

value = [(0,0, {'fieldname': something})]

self.update({

    'line_ids' : value,

})

the updated line_ids shows the value imidiately. 

in Odoo 16 i only see in the form that it has to be saved, nothing else.
I can add a value from the form itself, but not from code

in Odo 16 it doesnt seem possible anymore. did they change the way adding values in One2many fields?

hope you understand what i mean. 

thx for any help

Avatar
Zrušiť
Best Answer

In Odoo 16 and onwards, the mechanism for adding records to One2many fields via code has indeed changed slightly. Instead of directly using `self.update()` to modify One2many fields, you can use the recordset methods like `create` or `write`.


Here's how you can add records to a One2many field in Odoo 16+:


# Assuming self is a record of the main model where line_ids is a One2many field

# Create a recordset for the related model 'comodel'
comodel_obj = self.env['comodel']

# Prepare the values to be added to the One2many field
values_to_add = {
    'fieldname': something,  # Replace 'fieldname' and something with your actual field and value
    # Add other fields and values as needed
}

# Create a new record in 'comodel' and link it to the main record
new_record = comodel_obj.create(values_to_add)

# Add the newly created record to the One2many field 'line_ids'
self.write({
    'line_ids': [(4, new_record.id)]
})



This snippet demonstrates how to create a new record in the related model ('comodel') and then link it to the main record by appending its ID to the One2many field 'line_ids' using the `(4, id)` syntax.


Ensure to replace `'fieldname'` and `something` with your actual field name and value that you want to set in the 'comodel'.


This should enable you to add records to the One2many field programmatically in Odoo 16 and newer versions.

Avatar
Zrušiť
Autor

wow, thx so much Sayed Mohammed Aqeel Ebrahim for your answer. thats what i thought and found nothing in internet, or possible i searched not enough. :-)

I was looking for a resolution for days now. in the odoo versions lower then 15 it always worked.
thx a lot i will try this tomorrow

Autor

hello Sayed Mohammed Aqeel Ebrahim,
i tried it and it works partially.
it works on an already saved object, but not in an unsaved new object.
when i create a one2many line manually in the form ii appears immediately, and when i save the object the relation id is automatically written in the one2many object.

what i try to achieve is creating a new object and add some one2many line from code without the need of saving the object.
when i do it like you have written, the lines in one2many are written in database without the id of the parent object of the one2many. after saving the parent the id of the parent
is not in the one2many lines.

when i add some new one2many lines in an saved parent the parent id is available an can be written in the one2many line.

in odoo versions lower then 15 it was possible to add lines to a one2many field like
lines_ids = [(0,0, {'value': something})]
and the line was visible immediately.
hope it is understanable what i have written, my english is not the best :-)

Autor Best Answer

wow, thx so much for your answer. thats what i thought and found nothing in internet, or possible i searched not enough. :-) 

I was looking for a resolution for days now. in the odoo versions lower then 15 it always worked.

thx a lot i will try this tomorrow 


Avatar
Zrušiť
Related Posts Replies Zobrazenia Aktivita
1
sep 23
1882
1
jan 24
1897
1
dec 22
2747
1
jún 23
3544
2
jún 23
7177