2016-02-13 64 views
0

我是新的Google App Engine,因此我遵循Python的HelloWorld和Guestbook教程开始。Google App Engine - 在Python中搜索字符串属性

现在我想查询我的班级中的特定字符串,但在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) 

我想找个地方作家==“约翰·列侬”假设它们都属于同一个“流派”

的所有歌曲我尝试了一些东西像:

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/querieshttps://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=...) 

[] 

空单!

谢谢!

+1

为什么这个“显然”不是这样?如果你想查询他们,你为什么要将歌手属性索引为False? –

+0

什么是类song_key()应该做的。这有点奇怪 –

+0

我从留言板教程https://cloud.google.com/appengine/docs/python/gettingstartedpython27/usingdatastore开始,但你完全正确。我将歌手属性编入索引:) song_key()是音乐实体的“流派”。就像留言本教程中的“guestbook_key”一样,我使用歌曲键将所有歌曲条目收集到不同“流派”类别下。 我还添加了我的查询结果。 – Bikemat

回答

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)