简体   繁体   English

AttributeError:'bool'对象没有属性?

[英]AttributeError: 'bool' object has no attribute?

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'], 'patient_id':vals ['patient_id'],

KeyError: 'patient_id' KeyError:'patient_id'

If you are not modifying the field patient_id it will not be passed in the vals dictionary. 如果您没有修改字段patient_id,则不会在vals字典中传递。 If the patient_id has not been assigned in the UI it will be correctly assigned a value of False. 如果未在UI中分配patient_id,则会正确地为其分配值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. 如果您打印vals字典并查看日志或标准输出,您会发现patient_id可能为False,表示尚未分配。 I might do some evaluation before accessing its attrbutes. 在访问其attrbutes之前,我可能会做一些评估。

 'patient_id': vals['patient_id'] if vals.get('patient_id') else False,

'patient_id': vals['patient_id'], 'patient_id':vals ['patient_id'],

KeyError: 'patient_id' KeyError:'patient_id'

It simply highlight that key is not there in the vals and you are trying to access. 它只是突出显示val中没有键并且您正在尝试访问。 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. 建议始终使用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是绝对正确的,如果字段被修改,你只能获得写入方法的密钥,否则你将无法获得vals中的密钥。

In write method we only get the key value which is actually changed. 在write方法中,我们只获取实际更改的键值。 It may happens that the patient_id not changed in it. 可能会发生Patient_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字典中获取密钥时,我试图从当前记录中获取数据。

    @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)
@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 : 方法“写”不返回任何内容,您可以参考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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM