2017-07-31 180 views

回答

2

你有2個選擇這裏(我已經改變了nameTest在我的例子my_many2one_id!):

  1. 使用的onchange行爲
class MyModel(models.Model): 
    _name = 'my.model' 

    my_many2one_id = fields.Many2one(
     comodel_name='medical.lab.patient', 
     required=True 
    ) 
    my_field1 = fields.Boolean(string="My Field1") 
    my_field2 = fields.Char(string="My Field2") 

    @api.onchange('my_many2one_id') 
    def onchange_my_many2one_id(self): 
     self.my_field1 = self.my_many2one_id.my_field1 
     self.my_field2 = self.my_many2one_id.my_field2 
  • 使用相關領域
  • my_field3 = fields.Boolean(
         string="My Field3", related="my_many2one_id.my_other_field3") 
        my_field4 = fields.Char(
         string="My Field4", related="my_many2one_id.my_other_field4") 
    
    0

    使用

    @api.onchange(put_your_fields_here) 
    def onchange_custom(self): 
    
    return {'value': {'field_1': value1, 'field_2': value_2)} 
    
    +0

    ,但我怎麼能獲得與第一字段的值? – Borealis

    +0

    'self.first_field' –

    1

    你需要的是相關領域:

    讓利說有model.one

    _name = 'model.one' 
    
    field_1 = fields.Char(..) 
    field_2 = fields.Date(..) 
    

    和模型model.two有one2many場model.one

    _name = 'model.two' 
    
    many2one_field = fields.Many2one(co_model='model.one', ....) 
    # just add related field two use them in the view. 
    # here I chosed to give them the same name 
    # if you don't specify any attribute like string , required .... 
    # they inherit the attributes of the model.one fields 
    field_1 = fields.Char(related='many2one_field.field_1', 
             readonly=True) 
    field_2 = fields.Date(related='many2one_field.field_2', 
             readonly=True) 
    

    當您選擇many2one字段 時,會自動更改相關字段的工作,如代理或計算字段。

    ,如果你只是想填補該領域與相關領域的價值創造 onchange事件

    @api.onchange('many2one_field') 
        def onchange_many2OneField(self): 
         # first check many2one field have value 
         if self.many2one_field: 
          self.field_1 = self.many2one_field.some_field_1 
          self.field_2 = self.many2one_field.some_field_2 
         else: 
          # make them all False if it what you want 
          self.field_1 = False 
          self.field_2 = False 
    
    +0

    what if'model.one'field_1 = fields.Many2One(..)and field_2 = fields.Many2One(..) – Borealis

    +0

    我給你一個兩個模型的例子來理解那裏的想法是不需要編寫不需要的代碼field_1 = fields.Char(string ='field for field') – Cherif

    +0

    保持不變Type使用Many2one(related ='.....') – Cherif