2012-11-26 55 views
1
def get_songs(requested_username): 
    songs = db.GqlQuery("SELECT * FROM Song ORDER BY created DESC") 

Song db.Model實體具有username屬性。Python查詢 - 網絡應用程序等

如何找到只有一個特定用戶名的所有歌曲,如果歌曲的用戶名屬性等於請求的用戶名?

回答

2

你可以做到這一點有兩種方式:

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... 
+0

再次感謝你! :) #noobproblems –

+0

@RohitRayudu哈哈,根本不是noob - 你只是在學習新東西。祝你一切順利! – RocketDonkey

0

SELECT * FROM Song where username="Batman" ORDER BY created DESC 

你可能不希望使用蝙蝠俠在這種情況下