Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
6525 Lượt xem

Hi, 

This is an example:



class ModelA(models.Model):
         _name = 'model.A'

         type = fields.Boolean(string='Type')


class ModelB(models.Model):
         _name = 'model.B'

         type = fields.Selection([('1', 'One'),('2', 'Two')])

I want when i select 1 in the selection option in model B to change the default value of field 'type' in model A to true, and 2 to false

How can i do that

Thanks


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Check out the related fields. They are fields that obtain value based on another. The advantage is that you save coding the onchange event.

https://www.odoo.com/documentation/12.0/reference/orm.html#related-fields

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi, 

Both models should be linked. So we'll add a M2O field in model B like this: 

a_id = fields.Many2one('model.A')

Then consider adding a ochange like this: 

@api.onchange('type'):
def onchange_type(self):
if self.type == ' 1':
    self.a_id.type = True
else:
​    self.a_id.type = False

Hope this helps.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 24
2453
1
thg 6 24
4970
4
thg 3 24
3361
1
thg 10 23
5329
1
thg 10 23
10644