Chaban33
Chaban33

Reputation: 1382

check when trying to assign Boolean

ctx['location_ids'] = vals['location_ids'] 

I have a large function so I will not post it here, but the problem is when vals['location_ids'] have values as integer everything works smooth, but sometimes there are no values in vals['location_ids'] so it is False, and when it is False I get error.

ctx['location_ids'] = vals['location_ids']
TypeError: 'bool' object has no attribute '__getitem__'

how can I avoid it, maybe add hasattr?

Upvotes: 0

Views: 46

Answers (1)

aman kumar
aman kumar

Reputation: 3156

you should try to check first it's dictionary

if isinstance(vals, dict):
   ctx['location_ids'] = vals.get('location_ids', None)

Upvotes: 1

Related Questions