0

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!

mrmo123
  • 725
  • 1
  • 8
  • 23

1 Answers1

1

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))
Albert
  • 3,611
  • 3
  • 28
  • 52
  • thank you so much!!!!!!!!! i wish i could give you 100 upvotes. i feel like i understand gql/python more too. you da man. – mrmo123 Apr 19 '12 at 08:25
  • After looking at the code again today, I found the reason I wasn't able to get the one-to-many collection in `class main` was that I didn't have the nested for loop. So the answer to my question is in order to select a one-to-many collection, you need a for...in... inside the main class for...in... Thank you! – mrmo123 Apr 19 '12 at 20:06