0

我有一个名为Main.

class Main(db.Model):
  name = db.StringProperty()
  phone = db.StringProperty()

还有另一个通过一对多关系Photo引用的数据库。Main

class Photo(db.Model):
  main = db.ReferenceProperty(Main,collection_name='photos')
  photo1 = db.StringProperty()
  photo2 = db.StringProperty()

我想知道是否有人知道如何根据以下查询Main(示例如下)制作字典:

class Name(webapp.RequestHandler):
  def get(self):
    names = db.GqlQuery('SELECT * FROM Main WHERE name=:1 LIMIT 1','Alan')
    values = {'names':names}
    self.response.out.write(template.render('base.html', values))

所以上面查询的目标是找到名字'Alan'。我不知道该怎么做的查询的目标应该是检索photo1and photo2(when name='Alan') in theclass Photo并将其放入字典中。

在此先感谢您的帮助!

4

1 回答 1

1

使用您提供的信息来确定您想要实现的目标有点困难。也许你可以进一步澄清。但是现在,这就是你的意思吗?

class Name(webapp.RequestHandler):
  def get(self):
    names = db.GqlQuery('SELECT * FROM Main WHERE name=:1 LIMIT 1','Alan')
    photo_array = [] #initialize an array to store the photos dicts
    for name in names:
      for photos in name.photos: #go over all the Photo entities that reference 'Alan'
        #retrieve and put in a dictionary, and append to the photo_array
        photo_array.append({"photo1":photos.photo1, "photo2": photos.photo2})
    values = {'names':names, 'photos': photo_array}
    self.response.out.write(template.render('base.html', values))
于 2012-04-19T07:26:21.883 回答