Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

How to create a record for field using many2one in Odoo 13?

I have 3 models. I used One2many field o put the tree view of model 3 in form view of model 1 and 2.

1/ bao_hiem.py with one2many field:

  lstd_baohiem = fields.One2many('lich.su', 'name')
  thamchieu = fields.Char('Tham chi?u')

2/ dieu_chinh.py with one2many field:

  lstd_dieuchinh = fields.One2many('lich.su', 'name')
  thamchieu = fields.Char('Tham chi?u')

And 3/ lich_su.py with many2one field:

   name = fields.Many2one('bao.hiem')
   name_id = fields.Many2one('bao.hiem')
   thamchieu = fields.Char('Tham chi?u')

I want to pass the values (eg: 'name', 'thamchieu') from dieu_chinh.py to lich_su.py and auto record with all those values via a button. So that, in the user's form view of model 1 and 2 can show the recorded value also, called the activity history of 1 user. The code for the button like this:

        def chapthuan(self):
          giatri_lstd = self.env['lich.su']
           gt = {
             'name' : self.name.id,
             'thamchieu' : self.thamchieu,
             'thoigian' : self.thoigian
               }
        list_lstd_dieuchinh=[]
        for line in self.lstd_dieuchinh :
            art = {}
            art['name'] = line.name.id
            art['lstd_dieuchinh'] = line.lstd_dieuchinh
            art['thamchieu'] = line.thamchieu
            art['thoigian'] = line.thoigian
            list_lstd_dieuchinh.append((0, 0, art))
            
        gt.update({ # add the list of command to gt
            'thamchieu': list_lstd_dieuchinh
                })
        giatri_lstd = giatri_lstd.create(gt)
        return True 

After click the button 'chapthuan', it can pass the value and create the record with the field 'name', 'date'. But with the field: 'thamchieu' which has relation many2one with model2 is still not worked.

enter image description here

I must select manually in the form view of the user.

So how to create a record with many2one field?

Please help!

Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

https://www.odoo.com/documentation/13.0/reference/orm.html#create-update

You are trying to create records by (0, 0, {values}) to name_id by the way of x2many field.

But name_id is Many2one field. That's your programming error.

But the error log you showed happened when you are trying to remove a record that already links to another record. So what I said above is not the main problem... Anyway, I'm a little confused with your code, it looks not so good


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...