代码之家  ›  专栏  ›  技术社区  ›  Randy Tang

谷歌应用引擎:ndb排序属性

  •  3
  • Randy Tang  · 技术社区  · 12 年前

    我有以下型号:

    class Product(ndb.Model):
        name = ndb.StringProperty()
        bidTime = ndb.DateTimeProperty()
        price = ndb.IntegerProperty()
        ...
    

    我希望使用以下查询:

    productRanks = Product.query(Product.bidTime>=startDate,
                                 Product.bidTime<endDate).order(-Product.price).fetch()         
    

    哪里 startDate endDate 是日期时间对象。但我收到以下错误消息:

    第一个排序属性必须与应用不等式筛选器的属性相同

    如果我加上 Product.bidTime 在顺序中,则不会出现错误:

    .order(Product.bidTime, -Product.price)
    

    然而,排序后的结果可能是错误的(根据日期,而不是价格)。那么,问题出在哪里呢?

    1 回复  |  直到 12 年前
        1
  •  5
  •   Lipis    12 年前

    就上诉而言没有问题。它的行为是有据可查的。从文档中

    注意:由于应用程序引擎数据存储执行查询的方式,如果 查询在属性上指定不等式筛选器,并在上指定排序顺序 其他属性,不等式过滤器中使用的属性必须是 在其他属性之前排序。

    看见 https://developers.google.com/appengine/docs/python/datastore/queries#Sort_Orders

    在得到结果集后,您可能需要在内存中进行排序。