Datastore Tutorial

61 views
Skip to first unread message

Ben Woodhead

unread,
Apr 24, 2012, 1:24:05 AM4/24/12
to google-a...@googlegroups.com
Hey Everybody, 

I am new to python and the datastore so I am in the perfect place to document my questions to make it easier for the next person. I am going to avoid all the display stuff and just talk about getting data in and out of the datastore. In this example we are going to develop a backend database for books. 

What is the ancestry stuff and how should I really organize my data? Should we put everything into books and magazines?
Books -> book 1, book 2
Magazines -> mag 1, mag 2

What does that look like in python or java?
class Books(db.Model):
    pass

class Book(Books):
    name = db.StringProperty(required=True)
    created = db.DateProperty(auto_now_add=True)

How do I put that into the datastore?
book = Books(name="test book")
book.put()

How do you get the key of that book?
book.key()

How do you find all the books?
books = db.GqlQuery("SELECT * FROM Book WHERE ANCESTER IS Books ORDER BY created DESC LIMIT 10")

How do I loop through the results?
for book in books:
  print book.key() ????
  print book.name

Thanks, 
Ben
ps In the example they use the method below to create a key.. What does it really mean or do?
def guestbook_key(guestbook_name=None):
 
"""Constructs a Datastore key for a Guestbook entity with guestbook_name."""
 
return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')

Kaan Soral

unread,
Apr 24, 2012, 3:36:47 PM4/24/12
to google-a...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages