imad
imad

Reputation: 189

odoo Fill one2many from a model to another model

In order to transfert data from mro.request to mro.order I made a function named action_confirm and also to make transition for my workflow, the creation of a new object worked perfectly also the transfer of data from char,date... but in order to fill a one2many field it doesn't work and I get no error.

my button type is workflow I know that I'm missing something, just can't figure it out.below, you find my code... best regards

my models:

1/

 class mro_request(osv.osv):

    _name = 'mro.request'

    _columns = {
    'intervention': fields.one2many('fleet.service.type.line','intervention_id','Order réparations', help='Cost type purchased with this cost'),
    'priority'    : fields.selection([('0', 'Not urgent'), ('1', 'Normale'), ('2', 'Urgent'), ('3', 'Tres Urgent'),('4', 'Critique')], 'Priorité',
        select=True, readonly=True, states=dict.fromkeys(['draft', 'confirmed'], [('readonly', False)])),
    }

    @api.multi
    def action_confirm(self):
        or_object = self.env['mro.order']
        affectation_line = self.env['fleet.service.type.line']


    ## creating maintenance order object is working
        obj = {'state': 'draft', 'date_planned' : self.execution_date,'intervention': self.intervention,
            'asset_id' : self.asset_id.id, 'description': self.description}
        purchase_id = or_object.create(obj)  


        list_intervention=[]
        for line in self.intervention :
            art = {}

            art['desc'] = line.description
            art['intervention_type'] = line.intervention_type.name

            art_id = affectation_line.create(art) 
            list_intervention.append(art_id)

        self.write({'state': 'run'})

        return True

2/

 class mro_order(osv.osv):

    _name = 'mro.order'

    _columns = {
    'intervention': fields.one2many('fleet.service.type.line','intervention_id','Order réparations', help='Cost type purchased with this cost'),
    'priority'    : fields.selection([('0', 'Not urgent'), ('1', 'Normale'), ('2', 'Urgent'), ('3', 'Tres Urgent'),('4', 'Critique')], 'Priorité',
        select=True, readonly=True),
    }

3/

class fleet_service_type_line(osv.Model):
    _name = 'fleet.service.type.line'
    _description = 'Type of services available on a vehicle'
    _columns = {

    'intervention_id'   : fields.many2one('mro.request'),
    'intervention_type' : fields.many2one('fleet.service.type','Type Intervention'),
    'intervention_mro' : fields.many2one('mro.order','Type Intervention'),
    'user_id'           : fields.many2one('res.users', 'Chef Atelier', help="Workshop Chief"),
    'desc' : fields.char('desc'),

    }

Upvotes: 4

Views: 1586

Answers (1)

Charif DZ
Charif DZ

Reputation: 14721

try this:

 @api.multi
def action_confirm(self):
    or_object = self.env['mro.order']

    ## creating maintenance order object is working
    obj = {  
        'state': 'draft', 
        'date_planned' : self.execution_date,
        'intervention': self.intervention,
        'asset_id' : self.asset_id.id, 
        'description': self.description
    }

    # before you create your model add the list 
    # of records to be created in intervention
    list_intervention=[]
    for line in self.intervention :
        art = {}
        art['desc'] = line.description
        art['intervention_type'] = line.intervention_type.name
        # x2many field accept list of cammands  (command, value1, value2)
        # for create pass 0 in cammand and a dictionary in value2
        list_intervention.append((0, 0, art))

    obj.update({ # add the list of command to obj
        'intervention': list_intervention,
        })
    # now the create method will create the record in intervention too
    or_object = or_object.create(obj)        
    self.write({'state': 'run'})
    return True

Upvotes: 3

Related Questions