我是新的Google App Engine,所以我开始使用Python的HelloWorld和Guestbook教程。
现在我想查询我班级中的特定字符串,但是我在GAE文档中找不到合适的文档。
我有以下内容:
class song_key(genre)
#We use genre as the key for a Song entity
return ndb.Key('Repository',genre)
class Singer(ndb.Model):
name = ndb.StringProperty()
birthday = ndb.DateProperty()
class Song(ndb.Model):
author = ndb.StructuredProperty(Singer)
title = ndb.StringProperty()
release = ndb.DateProperty()
dateAdd = ndb.DateTimeProperty(auto_now_add=True)
我想找到作者=='John Lennon'的所有歌曲,假设他们都属于同一个'流派'
我尝试了一些类似的事情:
q = Song.query((Song.author.name == "John Lennon'), ancestor=song_key(genre)).order(-Song.data)
results = q.all()
但这显然不是执行此查询的方法。
有人可以帮我理解Google App Engine中的查询吗?实现这样一个stringProperty查询的好方法是什么?
我已经完成了https://cloud.google.com/appengine/docs/python/ndb/queries或https://cloud.google.com/appengine/docs/python/datastore/queryclass,但我有一些问题了解整个概念^^
编辑:这是两个查询的结果:
Query(kind='Song', ancestor=Key('Repository', 'Pop'), orders=...)
[Song(key=Key('Repository', 'Pop', 'Song', 5733953138851840), author=Singer(name=u'John Lennon'), date=datetime.datetime(2016, 2, 13, 22, 59, 31, 766373),title=u'Come Together'), Song(key=Key('Repository', 'Pop', 'Song', 5556931766779904), author=Singer(name=u'Bruno Mars'), date=datetime.datetime(2016, 2, 13, 16, 15, 58, 584174), title=u'Uptown Funk), Song(key=Key('Repository', 'Pop', 'Song', 6119881720201216), author=Singer(name=u'Katty Perry'), date=datetime.datetime(2016, 2, 13, 16, 15, 30, 915749), title=u'Teenage Dream')]
我有三个条目。
当我想按作者姓名过滤时:
Query(kind='Song', ancestor=Key('Repository', 'Pop'), filters=FilterNode('author.name', '=', 'John Lennon'), orders=...)
[]
清空清单!
谢谢!
答案 0 :(得分:0)
好的,我找到了解决问题的pythonic方法: 我查询特定类型的所有歌曲,然后检查我要找的歌手是否属于此列表。
q = Song(ancestor=repository_key(genre))
results = p.fetch()
songs = []
for result in results:
if author in result.author.name:
songs.append(result)