【问题标题】:One-to-many relationship in Datastore and de-referencing in Google App EngineDatastore 中的一对多关系和 Google App Engine 中的取消引用
【发布时间】:2009-01-28 17:17:57
【问题描述】:

我在两个实体之间有一对多的关系:第一个是卫星,第二个是频道。卫星表单返回一个卫星名称,我希望它出现在另一个 HTML 页面中,其中包含频道数据,您可以在其中说该频道与该卫星相关。

我该怎么做?

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    这听起来像是使用 App Engine 的 Datastore API 的一部分的 ReferenceProperty 的好案例。这是一个让您入门的想法:

    class Satellite(db.Model):
      name = db.StringProperty()
    
    class Channel(db.Model):
      satellite = db.ReferenceProperty(Satellite, collection_name='channels')
      freq = db.StringProperty()
    

    有了这个,你可以像这样分配频道:

    my_sat = Satellite(name='SatCOM1')
    my_sat.put()
    Channel(satellite=my_sat,freq='28.1200Hz').put()
    ... #Add other channels ...
    

    然后循环遍历给定卫星对象的通道:

    for chan in my_sat.channels: 
      print 'Channel frequency: %s' % (chan.freq)
    

    无论如何,这几乎遵循this article,它描述了如何在 App Engine 中建模实体关系。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多