0
votes

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

1
Try to create related field where you want get data from other modelAdam Strauss
Hi Adam. I'd tried to create a record with the field 'thamchieu' which has relation many2one in above like this --> self.env['lich.su'].create({'thamchieu': self.thamchieu}) . But in the UI, I got the error like this --> psycopg2.DataError: invalid input syntax for integer: "abc" . With 'abc' is the value of field 'thamchieu' was filled in the model 2. Please give me idea. Thank you!Leon Nguyen

1 Answers

0
votes

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