我要的是有一次我選擇的第一個字段與此相關的選擇等領域有自己的價值觀我怎麼能一次選擇一個領域的其他領域有價值?
nameTest = fields.Many2one(
comodel_name='medical.lab.patient',
required=True,
)
nameTest是一個對象,我怎麼能訪問該領域相關到這個對象?
我要的是有一次我選擇的第一個字段與此相關的選擇等領域有自己的價值觀我怎麼能一次選擇一個領域的其他領域有價值?
nameTest = fields.Many2one(
comodel_name='medical.lab.patient',
required=True,
)
nameTest是一個對象,我怎麼能訪問該領域相關到這個對象?
你有2個選擇這裏(我已經改變了nameTest
在我的例子my_many2one_id
!):
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")
使用
@api.onchange(put_your_fields_here)
def onchange_custom(self):
return {'value': {'field_1': value1, 'field_2': value_2)}
你需要的是相關領域:
讓利說有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
,但我怎麼能獲得與第一字段的值? – Borealis
'self.first_field' –