Odoo Help


How to Import one2many field record from one model to one2many field in another model

Nanda Kanoko
on 7/17/13, 5:29 AM 3,983 views

I need to import record saved from my o2m field to another o2m field in different model

class notebook_project(osv.osv):
    _name = "notebook.project"
    _description = "Notebook Project ID"

    def onchange_project_id(self, cr, uid, ids, project_id, arg, context=None):
        if project_id :
            prod = self.pool.get('project.project').browse(cr, uid, project_id, context=context)
            return {'value': {'name': prod.name}}
        return {}   

    _columns = {
        'name' : fields.char('Name', size=64),
        'project_id' : fields.many2one('project.project', 'Project'),
        'notebook_project_lines' : fields.one2many('notebook.project', 'notebook_project_id', 'Members Lines'),
        'notebook_project_id': fields.many2one('notebook.project', ondelete='cascade', select=True),
        'project_member' : fields.many2one('hr.employee', 'Members'),



Explanation :

I assigned some employee to certain project . When i see in my PostgreSQL , the notebook_project_id already record the ID that shows some employee already assigned to the project ID they assigned to . How can I call those record on different model ?

Ex : In first model , Project A have employee 1 , 2 , 3 . And on second model , when i choose project A and save , the field shows employee 1 , 2 , 3 on project A The models is somehow like mrp.bom and mrp.production . first model is mrp.bom and second model is mrp.production Please help !

ainur rofiq
On 7/18/13, 8:30 AM

I am not expert in python, there may be a better solution then this stupid code :)

class main_model(osv.osv)
    _name = "your.main.model"
    _columns = {
        'main_o2m':fields.one2many('some.model', 'field_id', 'Label', required=False),

    def create(self, cr, user, vals, context={}):
        #first model
        first_obj = self.pool.get('your.first.model')
        #browse and get o2m fields, according to your selected project(id)
        first_o2m = first_obj.browse(cr, user, [SELECTED_ID]).lines

        #copy first o2m model to second o2m model
        for line in first_o2m:
            vals['main_o2m'].append([0, False, {'employe_id':line.employe_id.id,}])

        return super(main_model, self).create(cr, user, vals, context)

hope this help. thanks

Here is the newest code i made , with error explanation in it . Please kindly check it , thanks . Link :http://stackoverflow.com/questions/17714692/copy-one2many-field-snafu

Nanda Kanoko
on 7/22/13, 2:16 AM

can u explain what is the selected ID ?

Nanda Kanoko
on 7/22/13, 6:25 AM

SELECTED_ID is 'id' of yout first model. this 'id' can be a field value in XML or a result of search condition as you want

KAM, ainur rofiq
on 7/22/13, 7:13 AM

well , according to my code posted in : http://stackoverflow.com/questions/17714692/copy-one2many-field-snafu ,, which one is the SELECTED_ID one ? because when i tried using notebook_project , it give Attribute Error

Nanda Kanoko
on 7/22/13, 7:28 AM

'project_id', but I get confused about your relation model. Is that work?

KAM, ainur rofiq
on 7/22/13, 7:44 AM

the truth is , i'm not really sure the relation is perfect , but ppl opinion said it's not wrong .. Actuallu it works , since it able to show the employee from first model ... but the problem is , the employee showed is not according to chosen project ,, the rest you can see at the web . Thanks anyway

Nanda Kanoko
on 7/22/13, 8:00 AM

tried it , it give an attribute error AttributeError: 'browse_record_list' object has no attribute 'lines'

Nanda Kanoko
on 7/22/13, 8:03 AM

that indicate method browse return a list.... try browse(cr, uid,......)[0].lines or if you use search method : project_id = obj.search(.....) then use project_id[0] as parameter in browse method. obj.browse(cr, uid, project_id[0])

KAM, ainur rofiq
on 7/22/13, 8:46 AM

added that , still got same error

Nanda Kanoko
on 7/23/13, 1:08 AM

I'm sory... I have no idea

KAM, ainur rofiq
on 7/23/13, 2:32 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.


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

1 follower(s)


Asked: 7/17/13, 5:29 AM
Seen: 3983 times
Last updated: 3/16/15, 8:10 AM