'python sqlalchemy query filter

I'm using SQLalchemy and have entered data into the database:

class Directions(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(16), index=True, unique=False)

Now, I'm trying to search for a given key:

Directions.query.filter(Directions.key=={some string})

But I get:

<flask_sqlalchemy.BaseQuery object at 0x103df57b8>

How do I uncover the actual string? I just spent two hours searching through the documentation, but that 1319 page haystack is not being useful.



Solution 1:[1]

Try using this:

direction = Directions.query.filter_by(key == <some string>).first()
print(direction)

Solution 2:[2]

The filter method return a BaseQuery object which you can chain multiple filters on it. You use first or all to get the results of current query.

Solution 3:[3]

You have to open up a session and use the query method of the session object. For example:

engine = create_engine(<db url>)
Session = sessionmaker(bind=engine)

with Session() as sess:
    sess.query(Directions).filter(Direction.key=={some string})

That code leading up to the Session call is described in the Session Documentation and might change for your application. You can read more about query objects in the docs as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HazemGomaa
Solution 2 stamaimer
Solution 3