Borealis
Borealis

Reputation: 1137

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'

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'

Upvotes: 3

Views: 41825

Answers (4)

Chavada Viki
Chavada Viki

Reputation: 1524

In write method we only get the key value which is actually changed. It may happens that the patient_id not changed in it.

So, that why we are not getting that key value from the vals dictionary.

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.

    @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)

Upvotes: 2

'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.

Upvotes: 2

Stone
Stone

Reputation: 111

@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 :

@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

Upvotes: 1

Phillip Stack
Phillip Stack

Reputation: 3378

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,

Upvotes: 4

Related Questions