I’m using SqlAlchemy to store some objects with a DateTime field:
my_date = Field(DateTime())
I’d like to run a query to retrieve the most recent few objects (Entities with the my_date field that are the most recent).
I’ve tried the following:
entities = MyEntity.query.order_by(MyEntity.time).limit(3).all()
entities = MyEntity.query.order_by(-MyEntity.time).limit(3).all()
But these queries return the same objects in the same order. The SqlAlchemy documentation notes the use of the ‘-‘ to reverse the order, but I’m surely missing something here.
Can anyone help?
You can do it like this:
You might need to:
Here’s some documentation.
Another option is this: