代码之家  ›  专栏  ›  技术社区  ›  Brendan

如何从Google App Engine数据存储查询中排序引用对象?

  •  3
  • Brendan  · 技术社区  · 17 年前

    我有 Exhibit 引用的对象 Gallery 对象,这两个对象都存储在Google App Engine数据存储中。

    我怎么点这个菜 展览 每本书的收藏 陈列室 对象,当我开始迭代这些值时(最终在Django模板中)?

    
    class Gallery(db.Model):
      title = db.StringProperty()
      position = db.IntegerProperty()
    
    class Exhibit(db.Model):
      gallery = db.ReferenceProperty(Gallery, collection_name='exhibits')
      title = db.StringProperty()
      position = db.IntegerProperty()
    
    galleries = db.GqlQuery('SELECT * FROM Gallery ORDER BY position')
    for gallery in galleries:
      gallery.exhibits.order('position')
    
    # ... send galleries off the the Django template
    
    

    在模板中呈现时,图库的顺序正确,但展品的顺序不正确。

    1 回复  |  直到 16 年前
        1
  •  4
  •   Brendan    17 年前

    您需要构造自己的查询,而不是依赖应用程序引擎创建的集合属性:

    Exhibles=Exhibly.all().filter(“gallery=,gallery”).order(“position”)

    或等效地,在GQL中:

    Exhibles=db.GqlQuery(“从gallery=:1按位置排序的Exhibly中选择*”,gallery)

    如果您希望能够从模板内部执行此操作,而不是传入展品列表,则可以在执行此查询的Gallery对象上定义一个简单的方法,并从模板引用它(例如,{Gallery.Exhibles_by_position}}将执行展品_by_position()在Gallery对象上,然后可以执行上述查询)。

    如果您担心这对速度的影响,不要担心:appengine创建的集合属性只是用于此的语法糖。