This question has been flagged
7 Replies
10183 Views

I have below list value, How can i return this list value into a one2many field?

I tried many ways but no output.

[{'current_location': (1, u'Location 1'), 'id': 60, 'location_type': u'own'}, {'current_location': (1, u'location 1'), 'id': 63, 'location_type': u'own'}]

My python code is :

ename = context.get('employee_id');

emp_obj = self.pool.get('hr.employee')
emp_name = emp_obj.search(cr, uid, [('name','=',ename)], context=context)
location_lines = []
location_ids = self.pool.get('employee.location').search(cr, uid, [('employee_id','=',emp_name)],limit=5)

In this location_ids all records based on employee_id  should be as a stored as list.

I want to display the values in list into my one2many field. I want to display the records. dont want to create or write.

I dont know how i overcome the situation. I just want to display values in location_ids into my one2many field in hr.employee. How can i do this using the button click function? Please help me.

 

 

 

 

Avatar
Discard
Author

hi

What do you mean by return? Looking at your code, employee.location must be linked to hr.employee through employee_id. I hope emp_name is hr.employee's ID. If so, displaying location_ids in the view alone should displayed all employee.location that is linked to the particular hr.employee. The add functionality is also build in if you use one2many_list widget. Where are you implementing this? Within the on_change mechanism? If it is for on_change mechanism and you want to add a value, use [(0, 0, {'current_location':r['current_location'], 'location_type':r['location_type']})]

Author

I'm using button click. when i click the button, all records from the employee.location based on employee_id will display in the one2many field in hr.employee.

Then I would suggest that the view that shows the location to be made for model hr.employee and only display location_ids in the view. If you pass the current active hr.employee's ID to that view in the window action, it should display the locations automatically.

Best Answer

Writing to a Many2one field :

(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)
(3, ID)                cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID)                link to existing record with id = ID (adds a relationship)
(5)                    unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

In this case use (6, 0, [ids])

Avatar
Discard