2013-03-11 66 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