一对多GQL查询并将结果放入字典中 [英] one-to-many GQL query and putting results into a dictionary
问题描述
我有一个名为 Main的Google数据库。
class Main (db.Model):
name = db.StringProperty()
phone = db.StringProperty()
还有另一个数据库 Photo
,它通过一对多的关系引用 Main
。
class图片(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'。我不知道该怎么做的查询的目标应该是检索 photo1
和 photo2
(当name = 'Alan')放在类照片
中并放到字典中。
在此先感谢您的帮助!
想要通过您提供的信息想要达到什么目标有点困难。也许你可以进一步澄清。但是现在,这是你的意思吗?
$ p $ class Name(webapp.RequestHandler):
def get(self ):
names = db.GqlQuery('SELECT * FROM Main WHERE name =:1 LIMIT 1','Alan')
photo_array = []#初始化一个数组来存储照片字典
为名字命名:
为name.photos中的照片:#go在引用Alan
#的所有照片实体上#gotrieve并放入字典中,并附加到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))
I have a google database called Main
.
class Main(db.Model):
name = db.StringProperty()
phone = db.StringProperty()
There's another database Photo
which references Main
through a one-to-many relationship.
class Photo(db.Model):
main = db.ReferenceProperty(Main,collection_name='photos')
photo1 = db.StringProperty()
photo2 = db.StringProperty()
I was wondering if anyone knows how to make a dictionary, based on the query of Main
(example shown below):
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))
So the goal of the query above is to find the name 'Alan'. The goal of the query I don't know how to do should be to retrieve photo1
and photo2
(when name='Alan') in the class Photo
and put it into a dictionary.
Thanks in advance for your help!
It's a bit difficult to figure out what you want to achieve with the information you provided. Perhaps you can clarify further. But for now, is this what you mean?
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))
这篇关于一对多GQL查询并将结果放入字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!