flask-jwt icon indicating copy to clipboard operation
flask-jwt copied to clipboard

AttributeError: 'dict' object has no attribute 'id'

Open sobolevn opened this issue 8 years ago • 4 comments

When passing {'id': 'someId'} into the _default_jwt_payload_handler it raises an exception.

The problem is in this expression: identity = getattr(identity, 'id') or identity['id'] https://github.com/mattupstate/flask-jwt/blob/master/flask_jwt/init.py#L53

It must be: identity = getattr(identity, 'id', None) or identity['id']

sobolevn avatar Feb 15 '17 09:02 sobolevn

I have a user model looks like this

class UserModel(db.Model):
    __tablename__ = 'b_users'
    ID = db.Column(db.Integer, primary_key=True)
    user_login = db.Column(db.String(120), nullable=False)
    user_pass = db.Column(db.String(120), nullable=False)
    user_nicename = db.Column(db.String(120), nullable=False)
    user_email = db.Column(db.String(120), nullable=False)
    user_url = db.Column(db.String(120), nullable=False)
    user_registered = db.Column(db.String(120), nullable=False)
    user_activation_key = db.Column(db.String(120), nullable=False)
    user_status = db.Column(db.Integer, nullable=False)
    display_name = db.Column(db.String(120), nullable=False)

instead of id I have ID in the object so I get

identity = getattr(identity, 'id') or identity['id'] AttributeError: 'UserModel' object has no attribute 'id'

Is there any work around to use custom id field in identity object ?

daverbj avatar Dec 09 '17 12:12 daverbj

I have just created a class instead of a dict to store my id as a property.

sobolevn avatar Dec 09 '17 21:12 sobolevn

I'm also getting this error. I'm using namedtuple as a workaround

Kartstig avatar Feb 03 '20 19:02 Kartstig

I would also like to use another property name, since id is a Python built-in function.

bartgenuit avatar Jun 28 '21 10:06 bartgenuit