Skip to Content
Menu
This question has been flagged
3 Replies
36459 Views

In stock.picking form, I would like to know how can I add an item to the one2many field move_lines using an onchange event on the partner_id field.

Both of the field with the onchange event and the one2many field that needs to add records belongs to the same class

Avatar
Discard
Best Answer

Hai Aldennis Lagumbay

I have solved it by the following code. Hope it helps you.

@api.onchange('customer_id')
def _onchange_customer_id(self):
    lines =[]
    for rec in self.customer_id.site_ids:
        result ={'name':rec.name}
        lines.append((0,0, result))
    self.site_ids = lines


Avatar
Discard
Best Answer

Hi Aldennis

suppose your one2manyfield name is my_field then

define list in first ex .. r=[]

then

r.append({'first_field_ino2m':'my vlaue','second......':'my second value'})

you can add more field availble in That one2manyobject for another record just continuous append same after all append completed just add rinmy_field':r,

exm. vals.update({'my_field':r})

Thanks
Sandeep

Avatar
Discard

This solution allows you to use the on_change even if you just clicked create and haven't saved your record yet. +1 and setted as accepted answerd. Thanks Sandeep

Thanks @ Sandeep +1

hello @Cluelogics @Camptocamp,

My problem in the following link. Can you provide me approprient solution for this problem.

https://www.odoo.com/nl_NL/forum/help-1/question/how-to-update-all-values-of-one2many-field-in-table-for-odoo10-125284

Does this still work in v14? Currently getting "unhashable type: 'dict'"
I know of the [(0,0,...)] method, but don't want to create them in the database before the first save as they won't delete if the parent object also isn't saved

hello , i still get the message : The operation cannot be completed:
- Create/update: a mandatory field is not set.
can anyone help me please

Best Answer

You need to return all IDs in one list with your one2many field in values.

Syntax:

return {'value': {'your_one2many_field': list_of_ids}}

For example my_emp_ids is my one2many field of hr.employee and I want to fill it when I will select Department. I want all employees of selected Depratment.

def onchange_department(self, cr, uid, deprt_id):
    employee_ids = self.search(cr, uid, [('department_id', '=', deprt_id)])
    return {'value': {'my_emp_ids': employee_ids}}
Avatar
Discard

I've tried your code , it's working . but the employee filled is not based on their department , but based on the id of class that contain deprt_id . Example : in my departments , Admin have ID 1 , Programmer have ID 2 , Engineer have ID 3 . When I choose Programmer Department , the printed employees on my_emp_ids is not programmer dept employee , but Admin dept employee . When I checked on PostgreSQL Database , the deprt_id shows correct id ( same id with department_id ) . I assume that the printed employee based on id of deprt_id and not the deprt_id . Am I making mistake ? please help

This code is full work Thank you @ Sudhir sir +1 and up voted.

Related Posts Replies Views Activity
1
Oct 23
1000
2
Oct 23
1319
2
Aug 23
2997
4
Aug 23
19390
3
Oct 22
9897