跳至内容
菜单
此问题已终结
3 回复
36894 查看

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

形象
丢弃
最佳答案

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


形象
丢弃
最佳答案

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

形象
丢弃

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

最佳答案

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}}
形象
丢弃

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.

相关帖文 回复 查看 活动
1
10月 23
1592
2
10月 23
1835
2
8月 23
3672
4
8月 23
20005
3
10月 22
10515