0

I have two models: Subject and Content. The Subject entity is the parent of the Content entity. For example, Biochemistry (subject) is the parent of intermediate metabolism (content) and nitrogen metabolism (content). I am having trouble querying these in a presentable formation. The goal is an output like: Biochemistry – carb met., nitrogen met., lipid met.; Immunology – innate, adaptive; English – a, b, c, d, etc. In the past I would just make one massive database that contained the information; but I think using the Parent/Child system will make the database more manageable. Any pointers would be greatly appreciated, I don't really know where to go from here.

def get(self):

  #Get all the Subjects
  subjects = ndb.gql('SELECT __key__ FROM Subject ORDER BY order ASC')
  subjectNames = ndb.gql('SELECT name FROM Subject ORDER BY order ASC')
  values = {'subjectNames':subjectNames}

  #Ancestor query
  values['contents'] = []
  for s in subjects:
    contents = Content.query(ancestor=s).fetch()
    values['contents'].extend(contents)
  self.response.out.write(template.render('1_home.html',values))
mrmo123
  • 725
  • 1
  • 8
  • 23
  • 1
    What is your actual problem, are you getting an error. One comment you don't need to recreate the key in the `Content.query` as you already have the complete key in `s` – Tim Hoffman May 18 '14 at 23:25
  • Thanks Tim for cleaning up my code with the suggestion! The problem now is that in the for s in subjects loop, the contents variable is being replaced so only the last one stays. I'll edit it in my question. – mrmo123 May 18 '14 at 23:34
  • 1
    values is only getting the last value based on your indenting. I assume you want a list of keys, rather than a list of lists of keys, so the answer below should be used with `extend` rather than append. – Tim Hoffman May 19 '14 at 00:20

1 Answers1

1

In that loop, you don't want to overwrite it each time, but to append to it. Make contents a list, then append to it inside the loop.

values["contents"] =[] for s in subjects: ... values["contents"].extend(the fetch)

GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • 1
    fetch returns multiple entities so `extend` might be the correct operation, rather than append. Each time through the loop `append` will add another list to the list. Resulting in a list of lists. – Tim Hoffman May 19 '14 at 00:19
  • 1
    Yes, Tim is correct. `values['contents'].extend(the fetch)` is correct. – GAEfan May 19 '14 at 00:34
  • Thanks for your help GAEfan and Tim. I was just wondering if you could help me with one more question. So I updated my post up top, and everything works fine except that I now have two lists within my dictionary (subjectNames and contents). Would you know how might I make it so that one subjectName corresponds with multiple contents? My ultimate goal was to have subjectName (Bio) content (carbs, proteins, etc.) subjectName (Immuno) content (innate, adaptive), and so on. Everything is ordered perfectly, I just need to match them up (one to many). Thanks again. – mrmo123 May 19 '14 at 06:35
  • 1
    Well, you would loop through the subjects, and build a dictionary that has len(subjects) elements, instead of 2 lists. `values = {'Bio': ['carbs', 'proteins', '...'], 'Immuno': [...], ...}`. Is that what you want? – GAEfan May 19 '14 at 13:26
  • Thanks GAEfan. I realized I had this issue two years ago: http://stackoverflow.com/questions/10222742/one-to-many-gql-query-and-putting-results-into-a-dictionary – mrmo123 May 21 '14 at 04:07