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

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

问题描述

  12345 
我有电影资料库,每个记录都有自己的ID,如下所示: 32453
12154
78873
34543

我想允许用户逐一浏览电影。首先,应该显示最新的电影(数据库的字段添加了带日期和时间)。如何从数据存储中获取?
Upd。我可以像下面这样做:

  movies = Movies.query )
movies.order(-Movies.added)
用于movie.fetch(1)中的电影:
self.response.out.write(movie.key.id())

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

其次,如果显示其他电影(例如,12154),用户应该能够转到前一个电影(ID32453)和下一个电影(ID78873)。当然,如果显示上一部电影,则不会有下一部电影;如果显示第一部电影,则不会有先前的电影。所以,问题是如何获得下一个和以前的电影的关键名称?
更新。如果当前显示的电影是12154,那么我应该为以前的电影和 example.com/movie/78873生成链接,如 example.com/movie/32453 为下一个。



更新。我尝试了如下所示:

  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:
prev_url = prev_movie.key.id()
else:
prev_url =''

但行不通... next_url 似乎没问题,但 prev_url 总是一样的。这里是我的测试数据库内容( -Movies.added order):

  id已添加
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(k如果prev_movie:
prev_url = prev_movie.id()
else:
prev_url =''
返回next_url,prev_url


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

12345
32453
12154
78873
34543

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.

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.

Upd. I've tried something like below:

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 = ''

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

解决方案

the following codes works well:

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天全站免登陆