How to easily retrieve all the entries of a class in the datastore?

31 views
Skip to first unread message

Lemo

unread,
Mar 31, 2012, 6:44:04 PM3/31/12
to google-a...@googlegroups.com
Hi, I'm pretty new to python / the google app engine, and I'm currently working on the online part of a desktop app I wrote.
One of the features is an image uploader, thats uses the Imgur API, and I would like to record every produced Imgur url into the datastore,
to create a giant mashup later with the python image processing library.
For example "ZCFc1", in the url http://imgur.com/ZCFc1
I'm testing with the following code (with an input like "?newlink=ZCFc1")

class Links(db.Model):
    link = db.StringProperty()

class MainPage(webapp2.RequestHandler):
  def get(self):
    addlink = self.request.get('newlink')
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write(addlink)
    Links(link = addlink).put()

Each entry is recorded correctly as "link" of the "Links" class,
but now I need to retrieve all of these links, and I still couldn't find a way to do that.
The documentation is mainly focused on reading a specific entry for a specific class,
but doesn't give much help with more global approaches.
I tried the following code, but it would only print something like
"<google.appengine.ext.db.Query object at 0x030D3390>"
instead of returning the actual contents

class Links(db.Model):
link = db.StringProperty()

class MainPage(webapp2.RequestHandler):
def get(self):
linklist = Links.all()
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write(linklist)
I saw the fetch() command, but how could I tell how many entries I need to retrieve in that case?
Also I thought of using a StringListProperty instead for a single list of links, but I would have to read and overwrite it each time,
and it doesn't seems as handy and reliable as having a separate entry for each link?

Thx for your help,
Lemo

Amy Unruh

unread,
Apr 2, 2012, 1:17:37 AM4/2/12
to google-a...@googlegroups.com
hi Lemo,

Once you've created your Query object, you need to execute it by calling the fetch() method (which optionally lets you specify the number of results to return); or by treating the Query object as an iterable.  See https://developers.google.com/appengine/docs/python/datastore/queryclass for more information.

By the way: this is the type of language-specific question that we're shifting over to Stack Overflow (see http://stackoverflow.com/questions/tagged/google-app-engine), where there is a very active community of people who monitor the 'google-app-engine' tag, so if you have a follow-up question, you might try posting it there.

 -Amy

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/G_Mq6uR0lX8J.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Reply all
Reply to author
Forward
0 new messages