Reputation: 1137
What I want is once I choose the first field the fields related to this choice have their values
nameTest = fields.Many2one(
comodel_name='medical.lab.patient',
required=True,
)
nameTest is an object how can I access the fields related to this object?
Upvotes: 2
Views: 1099
Reputation: 14751
what you need is related field :
let say there is model.one
_name = 'model.one'
field_1 = fields.Char(..)
field_2 = fields.Date(..)
and model model.two
that have one2many field to 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)
related field work like a proxy or a compute field when you select the many2one field there value is automatically changed.
and if you just want to fill the field with the value of the related field create onchange event
@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
Upvotes: 1
Reputation: 14801
You have 2 options here (i've changed nameTest
to my_many2one_id
in my examples!):
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")
Upvotes: 2
Reputation: 3747
Use
@api.onchange(put_your_fields_here)
def onchange_custom(self):
return {'value': {'field_1': value1, 'field_2': value_2)}
Upvotes: 0