Reputation: 23725
How can I modify this
videos = Video.gql( 'WHERE user_id = :1', '18' ).fetch(8)
so that instead of matching for a user_id, it returns any of the last 8 Video objects added to the datastore?
Upvotes: 2
Views: 174
Reputation: 2374
You should save your models with a
created = db.DateTimeProperty(auto_now_add=True)
for this type of queries.
The accepted answer on this question is not correct. If you add a created
property to your models and compare queries ordered by -created
and -__key__
you will see that they don't match.
Keys that are automatically set will not be monotonically increasing. Some good read on that here .
Upvotes: 1
Reputation: 879
Another workaround would be to have a property that store the date created, this would allow you to query a bunch of different queries including last created.
Example
class Video(db.Model):
...
user_id = ...
date_created = db.DateTimeProperty(auto_now_add=True)
...
retrieve last 8 additions:
Video.all().order("-date_created").fetch(8)
Besides this, you can do more interesting queries by date range an so on.
Hope this helps as well!
Upvotes: 2
Reputation: 16625
Try this:
Video.gql('ORDER BY __key__ DESC').fetch(8)
In fact, you don't need to use GQL
at all for this:
Video.all().order('-__key__').fetch(8)
Upvotes: 1