如何从数据存储中获取最新和相邻的记录? [英] How to get latest and adjacent records from the datastore?

查看:21
本文介绍了如何从数据存储中获取最新和相邻的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有电影数据存储,那里的每条记录都有自己的 ID 作为键名,如下所示:

I have movies datastore, each record there has its own id as key name like below:

12345
32453
12154
78873
34543

我想允许用户一一浏览电影.首先,应该显示最新的电影(数据库具有带有日期和时间的字段 added).如何从数据存储中获取它?更新.我可以这样做:

I would like to allow user to browse movies one by one. Firstly, the latest movie should be shown (database has field added with date and time). How to get that from the datastore? Upd. I can do it like below:

movies = Movies.query()
movies.order(-Movies.added)
for movie in movies.fetch(1):
    self.response.out.write(movie.key.id())

但我不喜欢它 - 为了获得密钥,我请求了整个记录.

But I don't like it - in order to get key I request the whole record.

其次,如果显示了其他电影(例如,12154),用户应该能够转到上一部电影(id 32453)和下一部电影(id 78873).当然,如果放映了最后一部电影,就没有下一部电影;如果显示第一部电影,则不会有前一部电影.那么,问题是如何获取下一部电影和上一部电影的关键名称?更新.如果当前显示的电影是 12154,那么我应该为上一部电影生成类似 example.com/movie/32453 的链接,为下一部电影生成example.com/movie/78873 之类的链接.

Secondly, if some other movie is shown (for ex., 12154), user should be able to go to previous movie (id 32453) and next movie (id 78873). Of course, if last movie is shown, there will not be next movie; and if first movie is shown, there will not be previous movie. So, the question is how to get key names of next and previous movies? Upd. If current movie shown is 12154, then I should generate links like example.com/movie/32453 for previous movie and example.com/movie/78873 for the next one.

更新.我试过如下:

next_movie = Movies.query(Movies.added < movie.added)
next_movie = next_movie.order(-Movies.added)
next_movie = next_movie.get()
if next_movie:
    next_url = next_movie.key.id()
else:
    next_url = ''

prev_movie = Movies.query(Movies.added > movie.added)
prev_movie = prev_movie.order(-Movies.added)
prev_movie = prev_movie.get()
if prev_movie:
    prev_url = prev_movie.key.id()
else:
    prev_url = ''

但效果不佳... next_url 似乎没问题,但 prev_url 始终相同.这是我的测试数据库内容(-Movies. added 顺序):

But it doesn't work well... next_url seems to be OK, but prev_url always the same. Here is my test database content (-Movies.added order):

id      added
503035: 2012-08-05 19:49:51.259000
475537: 2012-08-05 19:49:51.238000
677539: 2012-08-05 19:49:51.218000
566355: 2012-08-05 19:49:51.197000
557850: 2012-08-05 19:49:51.176000
670146: 2012-08-05 19:49:51.155000
581030: 2012-08-05 19:49:51.135000
464561: 2012-08-05 19:49:51.114000
507817: 2012-08-05 19:49:51.092000

推荐答案

以下代码效果很好:

next_movie = Movies.query(Movies.added < movie.added)
next_movie = next_movie.order(-Movies.added)
next_movie = next_movie.get(keys_only = True)
if next_movie:
    next_url = next_movie.id()
else:
    next_url = ''

prev_movie = Movies.query(Movies.added > movie.added)
prev_movie = prev_movie.order(Movies.added)
prev_movie = prev_movie.get(keys_only = True)
if prev_movie:
    prev_url = prev_movie.id()
else:
    prev_url = ''
return next_url, prev_url

这篇关于如何从数据存储中获取最新和相邻的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆