【问题标题】:Serializing ReferenceProperty in Appengine Datastore to JSON将 Appengine 数据存储中的 ReferenceProperty 序列化为 JSON
【发布时间】:2011-01-05 19:17:40
【问题描述】:

我正在使用以下代码将我的 appengine 数据存储区序列化为 JSON

class DictModel(db.Model):
    def to_dict(self):
        return dict([(p, unicode(getattr(self, p))) for p in self.properties()])


 class commonWordTweets(DictModel):
    commonWords = db.StringListProperty(required=True)
    venue = db.ReferenceProperty(Venue, required=True, collection_name='commonWords')

上课地点(db.Model): id = db.StringProperty(必需=真) FourSqid = db.StringProperty(必需=假) 名称 = db.StringProperty(必需 = True) twitter_ID = db.StringProperty(required=True)

这将返回以下 JSON 响应

 [
  {
    "commonWords": "[u'storehouse', u'guinness', u'badge', u'2011"', u'"new', u'mayor', u'dublin)']",
    "venue": "<__main__.Venue object at 0x1028ad190>"
  }
]

如何返回实际出现的场地名称?

【问题讨论】:

    标签: python json serialization google-cloud-datastore


    【解决方案1】:

    首先,虽然这不是你的问题,但强烈建议使用simplejson 生成 json,而不是尝试自己将结构转换为 json 字符串。

    为了回答您的问题,ReferenceProperty 仅充当对您的 Venue 对象的引用。所以你只需照常使用它的属性。

    尝试类似:

    cwt = commonWordTweets()   # Replace with code to get the item from your datastore
    d = {"commonWords":cwt.commonWords, "venue": cwt.venue.name}
    jsonout = simplejson.dumps(d)
    

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 2016-07-17
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 2010-10-26
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多