2017-12-05 1 views
2

many2many 필드 내용을 다른 클래스의 many2many 필드로 복사해야합니다.Many2many 필드 값을 다른 Many2many 필드로 복사

@api.multi 
def tester(self): 
    context = self._context.copy() 
    list = [] 
    confs = self.risque.rubrique_ids 
    for rec in confs: 
     list.append(rec.id) 
    self.env['test.test'].create({ 
     'nom_risque': self.risque.nom_risque, 
     'rubrique_ids': [0, 0, list] 
    }) 

    return { 
     'name': 'Evaluation', 
     'view_type': 'form', 
     'view_mode': 'tree,form', 
     # 'views': [{'view_mode': 'form', 'view_id': 'rub_id'}], 
     'res_model': 'test.test', 
     'type': 'ir.actions.act_window', 
     'res_id': self.id, 
     # 'target': 'new', 
     'flags': {'initial_mode': 'edit'}, 
     'context': context, 
    } 

내 XML 코드 :

<button name="tester" string="Evaluer" type="object" class="oe_highlight" /> 

그러나이 nom_risque과 rubrique_ids에 대한 마지막 하나를 반환합니다.

답변

1

안녕 다음 시도 지금은 희망 동작하는 예제도 :-), 내 의견을 이해하기 위해

@api.multi 
# even if it's trying out something, give it an understandable name 
def tester(self): 
    # seems to be a singleton call, so restrict it with self.ensure_one() 
    # no need to copy the context here, just pass it in the end 
    context = self._context.copy() 
    # odoo's recordsets has some cool functions, e.g. mapped() 
    # but that's only usefull on other scenarios 
    list = [] 
    confs = self.risque.rubrique_ids 
    for rec in confs: 
     list.append(rec.id) 
    # you will call this new record later so don't forget to 'save' it 
    self.env['test.test'].create({ 
     'nom_risque': self.risque.nom_risque, 
     # that's just plain wrong, read the __doc__ on models.BaseModel.write() 
     'rubrique_ids': [0, 0, list] 
    }) 

    return { 
     'name': 'Evaluation', 
     'view_type': 'form', 
     'view_mode': 'tree,form', 
     # 'views': [{'view_mode': 'form', 'view_id': 'rub_id'}], 
     'res_model': 'test.test', 
     'type': 'ir.actions.act_window', 
     # here is the mistake: take the id of your created record above 
     'res_id': self.id, 
     # 'target': 'new', 
     'flags': {'initial_mode': 'edit'}, 
     'context': context, 
    } 

을 시도하십시오

CZoellner @
@api.multi 
def create_test_record(self): 
    self.ensure_one() 
    test = self.env['test.test'].create({ 
     'nom_risque': self.risque.nom_risque, 
     'rubrique_ids': [(6, 0, self.risque.rubrique_ids.ids)] 
    }) 

    return { 
     'name': 'Evaluation', 
     'view_type': 'form', 
     'view_mode': 'tree,form', 
     'res_model': 'test.test', 
     'type': 'ir.actions.act_window', 
     'res_id': test.id, 
     'flags': {'initial_mode': 'edit'}, 
     'context': self.env.context 
    } 
+0

가 감사를 :) 내가 가진 또 다른 또 다른 문제의 문제는 당신이 나를 도울 수 있습니까 – Guesmi

+0

여기에 대답 대신 새로운 질문을 만들어야합니다 ;-) – CZoellner

+0

괜찮습니다. 작성하겠습니다. – Guesmi

관련 문제