这是我的功能:
@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

这是错误:
属性错误:' bool'对象没有属性' patient_id'
编辑:
@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)

新错误是:
' patient_id':vals [' patient_id'],
KeyError:' patient_id'
答案 0 :(得分:4)
如果您没有修改字段patient_id,则不会在vals字典中传递。如果未在UI中分配patient_id,则会正确地为其分配值False。如果您打印vals字典并查看日志或标准输出,您会发现patient_id可能为False,表示尚未分配。在访问其attrbutes之前,我可能会做一些评估。
'patient_id': vals['patient_id'] if vals.get('patient_id') else False,
答案 1 :(得分:2)
在write方法中,我们只获取实际更改的键值。 可能会发生Patient_id没有改变的情况。
那就是为什么我们没有从vals字典中获取该键值。
试试这段代码。
在这段代码中,当我们没有在vals字典中获取密钥时,我试图从当前记录中获取数据。
@api.multi
def write(self, vals):
if vals.get('Amount',False) and vals['Amount'] > 0:
vals.update({'Amount_date':fields.Datetime.now())
self.env['medical.journal'].create({
'patient_id': vals.get('patient_id',False) and vals['patient_id'] or (self.patient_id and self.patient_id.id or False),
'cat': valds.get('cat',False) and vals['cat'] or (self.cat or False),
'test_type_id': vals.get('test_type_id',False) and vals['test_type_id'] or (self.test_type_id and self.test_type_id.id or False),
'state_money':vals.get('state_money',False) and vals['state_money'] or (self.state_money or False),
'Amount_in_date':vals.get('Amount_date',False) and vals['Amount_date'] or (self.Amount_date or False),
'type_In': "Reste",
'Amount_In':vals.get('Amount',False) and vals['Amount'] or (self.Amount or False),
'user_id': vals.get('user_id',False) and vals['user_id'] or (self.user_id and self.user_id.id or False),
'type_lev1': "IN",
})
return super(MedicalLab, self).write(vals)
答案 2 :(得分:1)
@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.id,
'cat': record.cat,
'test_type_id': record.test_type_id.id,
'state_money':record.state_money,
'Amount_in_date':record.Amount_date,
'type_In': "Reste",
'Amount_In':record.Amount,
'user_id': record.user_id.id,
'type_lev1': "IN",
})
return record
方法"写"不返回任何内容,你可以参考https://www.odoo.com/documentation/10.0/reference/orm.html#odoo.models.Model.write,所以如果你想在写完后获得最新的数据,最好重新浏览数据然后创建医学实验室,所以我建议:
@api.multi
def write(self, vals):
if vals['Amount'] > 0:
vals['Amount_date'] = fields.Datetime.now()
res=super(MedicalLab, self).write(vals)
record=self.browse(self.ids[0])
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 res