def get_songs(requested_username):
songs = db.GqlQuery("SELECT * FROM Song ORDER BY created DESC")
Song db.Model實體具有username屬性。Python查詢 - 網絡應用程序等
如何找到只有一個特定用戶名的所有歌曲,如果歌曲的用戶名屬性等於請求的用戶名?
def get_songs(requested_username):
songs = db.GqlQuery("SELECT * FROM Song ORDER BY created DESC")
Song db.Model實體具有username屬性。Python查詢 - 網絡應用程序等
如何找到只有一個特定用戶名的所有歌曲,如果歌曲的用戶名屬性等於請求的用戶名?
你可以做到這一點有兩種方式:
GQL
def get_songs(requested_username):
songs = db.GqlQuery('SELECT * FROM Song WHERE username=:1 ORDER BY created DESC', requested_username)
return songs
查詢實例:
def get_songs(requested_username):
songs = Song.all()
songs.filter('username =', requested_username).order('-created')
return songs
在這兩種情況下,songs
都將包含查詢結果,您可以遍歷它並根據需要訪問裏面的對象。
songs = get_songs(requested_username)
for song in songs:
# Do stuff here...
像
SELECT * FROM Song where username="Batman" ORDER BY created DESC
你可能不希望使用蝙蝠俠在這種情況下
建議你檢查出的文檔 - >https://developers.google.com/appengine/docs/python/datastore/queries#Filters
但是,爲了回答你的問題,這樣的事情:
def get_songs(requested_username, limit=10):
query = db.GqlQuery("SELECT * FROM Song WHERE username = :1 ORDER BY created DESC", requested_username)
songs = query.fetch(limit)
return songs
再次感謝你! :) #noobproblems –
@RohitRayudu哈哈,根本不是noob - 你只是在學習新東西。祝你一切順利! – RocketDonkey