this is my function:
@api.multi
def write(self, vals):
if vals['Amount'] > 0:
vals['Amount_date'] = fields.Datetime.now()
record=super(MedicalLab, self).write(vals)
self.env['medical.journal'].create({
'patient_id': record.patient_id,
'cat': record.cat,
'test_type_id': record.test_type_id,
'state_money':record.state_money,
'Amount_in_date':record.Amount_date,
'type_In': "Reste",
'Amount_In':record.Amount,
'user_id': record.user_id,
'type_lev1': "IN",
})
return record
this is the error:
AttributeError: 'bool' object has no attribute 'patient_id'
EDITS :
@api.multi
def write(self, vals):
if vals['Amount'] > 0:
vals['Amount_date'] = fields.Datetime.now()
self.env['medical.journal'].create({
'patient_id': vals['patient_id'],
'cat': vals['cat'],
'test_type_id': vals['test_type_id'],
'state_money':vals['state_money'],
'Amount_in_date':vals['Amount_date'],
'type_In': "Reste",
'Amount_In':vals['Amount'],
'user_id': vals['user_id'],
'type_lev1': "IN",
})
return super(MedicalLab, self).write(vals)
the new error is:
'patient_id': vals['patient_id'],
KeyError: 'patient_id'
If you are not modifying the field patient_id it will not be passed in the vals dictionary. If the patient_id has not been assigned in the UI it will be correctly assigned a value of False. If you print the vals dictionary and review your logs or standard output you should find patient_id is likely False indicating it has not been assigned. I might do some evaluation before accessing its attrbutes.
'patient_id': vals['patient_id'] if vals.get('patient_id') else False,
'patient_id': vals['patient_id'],
KeyError: 'patient_id'
It simply highlight that key is not there in the vals and you are trying to access. Because you have accessed it through the [ ] indices then key must be there, if you want to do that then you should try following way.
'patient_id': vals.get('patient_id', False),
It will check first for the key in dictionary if the key is not found then it will return the default value which you have specified in next argument. It's advisable to use get method always.
@Phillip Stack is absolutely correct you will only get the key in write method if the field is modified otherwise you will not get that key in vals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With