def get_songs(requested_username):
songs = db.GqlQuery("SELECT * FROM Song ORDER BY created DESC")
Song db.Model 实体有一个用户名属性。
如果一首歌的用户名属性等于请求的用户名,我如何从一个特定的用户名中找到所有歌曲?
def get_songs(requested_username):
songs = db.GqlQuery("SELECT * FROM Song ORDER BY created DESC")
Song db.Model 实体有一个用户名属性。
如果一首歌的用户名属性等于请求的用户名,我如何从一个特定的用户名中找到所有歌曲?
你可以通过两种方式做到这一点:
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