monmonja
monmonja

Reputation: 2264

datastore get previous/next appengine

In MySQL you can get the next or previous record based on the key/id. Anyone know how i can achieve this in app engine (im using python) by only knowing/passing the key? Thanks

Upvotes: 0

Views: 434

Answers (3)

Jose Leonel Boa
Jose Leonel Boa

Reputation: 11

This worked for me:

q = Noticia.all().filter("tipo",tipo).filter('____key____ <', bookmark).order("-____key____")

next = q.get()

q = Noticia.all().filter("tipo",tipo).filter('____key____ >', bookmark)

prev = q.get()

Upvotes: 1

Nick Johnson
Nick Johnson

Reputation: 101149

Assuming that by 'next/previous', you mean 'the entity with the key that is lexicographically closest to a given key', yes, you can do this using queries. Supposing your key is in 'my_key', and your model is called MyModel, in Python it would work like this:

q = MyModel.all().filter('__key__ >', my_key)
next_entity = q.get()

q = MyModel.all().filter('__key__ <', my_key)
prev_entity = q.get()

Upvotes: 2

Jason Hall
Jason Hall

Reputation: 20920

There's no way (by default) for an entity to know the next/previous entity.

One thing you could do is give each model a timestamp for the time they were created, then getting the next entity is as easy as selecting the first entity whose timestamp is after XXX.

Upvotes: 2

Related Questions