odoo Fill one2many 从一个模型到另一个模型

odoo Fill one2many from a model to another model

为了将数据从 mro.request 传输到 mro.order,我创建了一个名为 action_confirm 的函数,并为我的工作流程进行转换,创建一个新对象也非常有效来自 chardate... 的数据传输但是为了填充 one2many 字段它不起作用而且我没有收到任何错误。

我的按钮类型是 workflow 我知道我遗漏了一些东西,只是想不通 out.below,你找到我的代码...最好的问候

我的模特:

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

    }

试试这个:

 @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