2013-03-11 26 views
3

我有以下型号:谷歌应用程序引擎:NDB sort属性

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

我likd使用下面的查询:

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

其中startDateendDate是datetime对象。但我得到了以下错误消息:

第一次排序属性必须是相同的是应用

不等式过滤器。如果我的顺序添加Product.bidTime那么就不会有错误的性质:

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

然而,排序的结果将是错误的(根据日期,而不是价格)。那么,问题是什么?

回答

5

就appengine而言,没有问题。它的行为如同文件记录。从文档

Note: Because of the way the App Engine Datastore executes queries, if a query specifies inequality filters on a property and sort orders on other properties, the property used in the inequality filters must be ordered before the other properties.

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

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

+1

谢谢。所以我必须自己实现排序算法(例如,Bubble排序;-)。这太糟糕了。 – 2013-03-11 11:10:41

+1

嗯,不,你不必实现自己的排序算法,尽管你可以如果你不想阅读文档;-)。假设你的结果集不是很大,只需使用python的内置排序功能即可。请记住,如果您对结果集执行fetch(),则会返回列表。即使它是一个庞大的集合使用已经可用的设施。这是在Python中排序的一个很好的起点。 http://wiki.python.org/moin/HowTo/Sorting/ – 2013-03-11 11:16:27

+0

谢谢,我会试试看。 – 2013-03-11 11:38:31