2017-11-25 190 views
4

爲了將數據從mro.request轉換爲mro.order我創建了一個名爲action_confirm的函數,並且爲我的工作流程創建了一個轉換,新對象的創建也可以完美地傳遞數據chardate ...但爲了填補one2many字段,它不起作用,我沒有得到任何錯誤。odoo將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'), 

    } 
+0

你嘗試什麼@Zety這裏提出https://stackoverflow.com/questions/39524632/how-to-automatically-fill-a-one2many-字段值到另一個2many-field-in-o – user3676872

+0

它沒有工作。 – imad

回答

3

試試這個:

@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 
+0

非常感謝你,其實我找到了這樣的解決方案,但我不知道如何使用......再次感謝。你已經救了我的一天。 – imad

+1

歡迎你來這裏幫忙^^ – Cherif

相關問題