콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
3857 화면

I have a primary model that is related to a secondary model through a one2many relation.  Records in the secondary model are created programatically and need to be edited before saving. 

When I save the form, the relation with the edited records is lost (they are saved in the database but the ID of the primary model is empty.). If i do not edit the records the form is saved correctly. 

If i create the one2many records manually in the form, i can edit and they are saved correctly. 

Below is the code used to create the records in the one2many relation.  I cant use the ID of the primary model because before saving the id doesn't exist.

 What is the correct way of creating this records so the relation with the primary is not lost?

@api.one

    @api.onchange('cash_journal_id')
    def onchange_cash_journal_id(self):
        self.opening_details_ids=[]
        records={'pieces': 1}
        self.opening_details_ids=self.opening_details_ids.create(records)
        records={'pieces': 5}
        self.opening_details_ids+=self.opening_details_ids.create(records)
아바타
취소
베스트 답변

Hello Steve,

I have checked and I found that you need to put below code for one2many field.

Here records = {'pieces': 1}

self.opening_details_ids: [(0, 0, records)]

Here records = [{'pieces': 1}, {'pieces': 2}, {'pieces': 3}]

self.opening_details_ids: [(0, 0, rec) for rec in records]

Hope this will help you.

Thanks.

아바타
취소
관련 게시물 답글 화면 활동
2
4월 20
3684
0
11월 22
3407
2
9월 21
16389
0
7월 21
3153
1
2월 19
3370