0

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

Leon Nguyen
  • 187
  • 1
  • 16
  • Try to create `related` field where you want get data from other model – Adam Strauss Jan 08 '21 at 11:31
  • 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 Jan 09 '21 at 04:14

1 Answers1

0

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

  • Hi Nhat, I got the same error inform when I change name_id to One2many. But if I change name_id to field name. It can create the record, but not with username, so that it can not show in the tree view of model1 or model2' form view – Leon Nguyen Jan 06 '21 at 08:44
  • Hi Nhat, I'd update the button's code and the new issue I got in UI. – Leon Nguyen Jan 06 '21 at 10:06
  • There was something deadly wrong in your code. Your `or_object` is 'lich.su'. Your obj after all will be something looks like `{'name': [(0, 0, {value})], 'thamchieu': self.thamchieu}`. or_object can not create a record with obj, or it will behave wrong. You should learn ORM harder, before you code. This is so basic. – Nguyễn Minh Nhật Jan 07 '21 at 01:11