Rohit Rayudu
Rohit Rayudu

Reputation: 3951

Python Queries - Web Apps, etc

def get_songs(requested_username):
   songs = db.GqlQuery("SELECT * FROM Song ORDER BY created DESC")

The Song db.Model entity has a username property.

How do I find all the songs from only one specific username, if the a song has a username property equal to the requested username?

Upvotes: 1

Views: 64

Answers (3)

Gwyn Howell
Gwyn Howell

Reputation: 5424

Suggest you check out the documentation -> https://developers.google.com/appengine/docs/python/datastore/queries#Filters

But to answer your question, something like this:

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

Upvotes: 0

RocketDonkey
RocketDonkey

Reputation: 37249

You can do it in two ways:

GQL

def get_songs(requested_username):
    songs = db.GqlQuery('SELECT * FROM Song WHERE username=:1 ORDER BY created DESC', requested_username)
    return songs

Query instance:

def get_songs(requested_username):
    songs = Song.all()
    songs.filter('username =', requested_username).order('-created')
    return songs

In both cases, songs will contain the results of the query, and you can iterate over it and access the objects inside as needed.

songs = get_songs(requested_username)
for song in songs:
  # Do stuff here...

Upvotes: 2

Christian Thieme
Christian Thieme

Reputation: 1124

something like

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

you may not want to use Batman in this case

Upvotes: 0

Related Questions