Se rendre au contenu
Menu
Cette question a été signalée
1 Répondre
11812 Vues

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'),

        }

notebook_project()

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 !

Avatar
Ignorer
Meilleure réponse

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

Avatar
Ignorer
Auteur

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

Auteur

can u explain what is the selected ID ?

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

Auteur

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

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

Auteur

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

Auteur

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

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])

Auteur

added that , still got same error

I'm sory... I have no idea

Publications associées Réponses Vues Activité
0
sept. 20
2224
0
avr. 16
2647
3
mars 16
13249
1
avr. 23
4439
7
avr. 23
18496