How to get objects from model.User queried by Flask_SQLAlchemy

the result of the query is a model.user type, not a simple dict, so how can you use it like an object?

user = User.query.filter(User.username == username, User.password == password).first()

if you want to use user ["property"], you can use introspection to turn an object into dict, and generally speaking, objects are more convenient to have intelligent prompts and constraints. (if dict gets the value directly with the key in python, there is no:)), that will report an error unless you are accustomed to the way JS is written


.

you can take a look at this article to json sqlalchemy data .

of course, if you want to learn sqlalchemy, you can take a look at my project sql_to_sqlalchemy

.
def to_dict(model):
    return {c.name: getattr(model, c.name) for c in model.__table__.columns}
    
user = User.query.filter(User.username == username, User.password == password).first()
user_dict = to_dict(user)
Menu