2016-02-13 70 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)