Book Image

Mastering Google App Engine

Book Image

Mastering Google App Engine

Overview of this book

Table of Contents (18 chapters)
Mastering Google App Engine
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Querying your data


Before we can query some data, we should have some data. So, our first step will be to generate some sample data on which we can experiment with the query API that is provided by datastore. We will take our old Listing model from the previous chapters and add a few more fields to it. Next, we will create some listings for cars from 1990 to 2000 that come with a random price assigned to each.

So, the following is the program that does this:

from google.appengine.ext import ndb 
import random 

class Listing(ndb.Model): 
    title = ndb.StringProperty() 
    year = ndb.IntegerProperty() 
    price = ndb.IntegerProperty(default=0) 

for year in xrange(1990, 2001): 
    car = Listing() 
    car.title = "Honda Civic %s" % year 
    car.price = random.randint(4000, 50000) 
    car.year = year 
    car.put() 

So, as you can see, we have a Listing model with title, year, and price. We created a Listing instance, assigned a title to it, set a random price, and the year was assigned...