[Django]-TypeError: 'User' object is not subscriptable

2👍

validated_data['target'] gives you the value of the target field – which, as the error says, is an instance of User. You use normal attribute syntax to access its fields.

target_email = serializer.validated_data['target'].email

1👍

I was using fastapi.

class UserDetails(BaseModel):
    email: str
    price: int
   
@app.post ("/create_checkout_session/")
async def create_checkout_session(user_details:UserDetails):
    price = user_details.price
   

I got this error:

price = user_details['price']
     
TypeError: 'UserDetails' object is not subscriptable

Instead of passing key I used dot operator (.) to access value:

price = user_details.price

Leave a comment