Python AttributeError: 'bool' 对象没有属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44702166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'bool' object has no attribute?
提问by Borealis
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'
AttributeError: 'bool' 对象没有属性 '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'
'patient_id': vals['patient_id'],
密钥错误:“患者 ID”
回答by Phillip Stack
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 字典中传递。如果没有在 UI 中分配patient_id,它将被正确分配一个值False。如果您打印 vals 字典并查看您的日志或标准输出,您应该会发现 Patient_id 可能为 False,表明它尚未分配。在访问其属性之前,我可能会做一些评估。
'patient_id': vals['patient_id'] if vals.get('patient_id') else False,
回答by Emipro Technologies Pvt. Ltd.
'patient_id': vals['patient_id'],
KeyError: 'patient_id'
'patient_id': vals['patient_id'],
密钥错误:“患者 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.
它只是突出显示该密钥不在 vals 中并且您正在尝试访问。因为您已经通过[ ]索引访问了它,那么密钥必须在那里,如果您想这样做,那么您应该尝试以下方式。
'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.
如果未找到键,它将首先检查字典中的键,然后它将返回您在下一个参数中指定的默认值。建议始终使用 get 方法。
@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.
@Phillip Stack 是绝对正确的,如果字段被修改,您只会在 write 方法中获得密钥,否则您将无法在 vals 中获得该密钥。
回答by Chavada Viki
In write method we only get the key value which is actually changed. It may happens that the patient_id not changed in it.
在 write 方法中,我们只获取实际更改的键值。可能会发生其中的患者 ID 未更改的情况。
So, that why we are not getting that key value from the vals dictionary.
所以,这就是为什么我们没有从 vals 字典中获取该键值的原因。
try this code.
试试这个代码。
in this code i tried to get the data from the current record which is in self right now when we are not getting the key in vals dictionary.
在这段代码中,我试图从当前记录中获取数据,当我们没有在 vals 字典中获取键时,它现在处于 self 状态。
@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)
回答by Stone
@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
method "write" does not return anything, you can refer to https://www.odoo.com/documentation/10.0/reference/orm.html#odoo.models.Model.write, so if you want to get the latest data after write, better to re-browse the data then create medicallab, so i'm suggesting :
方法“write”不返回任何东西,你可以参考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