Fix error: object is not subscriptable

Generally this error occurs when we retrieve data from a model using get() method and try to access data as dictionary keys. get() method is used when we know that there is only record matching the condition and we want to retrieve a single object.

 

For example:

userDetails = Users.objects.get(pk=123)

If you try to access data as userDetails[‘fieldName’], it will give an error: “Users object is not subscriptable

 

You can access data from object returned from get() method like 

userDetails.fieldName

For example: userId = userDetails.id

userName = userDetails.name

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top
Shares