Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding ListProperty backend behavior in GAE

I'm trying to understand how you're supposed to access items in a GAE db.ListProperty(db.Key).

Example:

A Magazine db.Model entity has a db.ListProperty(db.Key) that contains 10 Article entities. I want to get the Magazine object and display the Article names and dates. Do I make 10 queries for the actual article objects? Do I do a batch query? What if there's 50 articles? (Don't batch queries rely on the IN operator, which is limited to 30 or fewer elements?)

like image 982
Yarin Avatar asked Nov 21 '11 18:11

Yarin


1 Answers

So you are describing something like this:

class Magazine(db.Model):   
    ArticleList = db.ListProperty(db.Key)

class Article(db.Model):
    ArticleName = db.StringProperty()
    ArticleDate = db.DateProperty()

In this case the simplest way to grab the listed articles is to use the Model.get() method, which looks for a key list.

m = Magazine.get() #grab the first record

articles = Article.get(m.ArticleList) #get Articles using key list

for a in articles:
    name = a.ArticleName
    date = a.ArticleDate
    #do something with this data

Depending on how you plan on working with the data you may be better off adding a Magazine reference property to your Article entities instead.

like image 133
Kevin P Avatar answered Sep 22 '22 09:09

Kevin P