将密钥更改为谷歌应用引擎中的字典 - 使用不同的方法解决

时间:2014-05-21 21:30:53

标签: python google-app-engine app-engine-ndb

我无法将键值更改为字典值

def get(self):

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

  #Get all the Contents
  for subject in subjects:
    contents = ndb.gql('SELECT * FROM Content WHERE ANCESTOR IS :1 ORDER BY order ASC',subject.key)
    values[subject.name] = contents #***HERE is the issue***

而不是获取字典

value = {key:value}

我想要

value = {{key:value}:value}

提前感谢任何建议!


修改 当我尝试

values['subject':subject.name] = contents

我收到错误

TypeError: unhashable type


使用不同的方法

解决了

def get(self):
  #Get all the Subjects
  subjects = ndb.gql('SELECT name,order FROM Subject ORDER BY order ASC')
  values = {'subjects':subjects}
  #Get all the Contents
  values['contents'] = []
  for subject in subjects:
    #Formatting HTML output
    subjectAll = subject.name + ' ' + subject.order
    contents = ndb.gql('SELECT name,order FROM Content WHERE ANCESTOR IS :1 ORDER BY order ASC',subject.key)
    values['contents'].append(subjectAll)
    for content in contents:
      #Formatting HTML output
      contentAll = content.name + '  ' + content.order
      values['contents'].append(contentAll)

0 个答案:

没有答案