appengine python NDB 排序顺序无效
appengine python NDB sort order has no effect
我尝试在 model.py 中使用这些模型获取最后修改的条目:
class Example(ndb.Model):
....
modified = ndb.DateTimeProperty(auto_now=True)
created = ndb.DateTimeProperty(auto_now_add=True)
查询代码:
for each in ['Example', '...']
model_class = webapp2.import_string('model.%s' % each)
q = model_class.query()
q.order(-model_class.modified)
last_modified_entity = q.get() # does not sort the entities
我也试过:
for each in ['Example', '...']
model_class = webapp2.import_string('model.%s' % each)
q = model_class.query()
sort_prop = ndb.GenericProperty('modified')
q.order(-sort_prop)
last_modified_entity = q.get() # does not sort the entities
在 Query 对象上调用 .order()
不会改变 Query 对象。它 returns 一个带有该订单的新 Query 对象,但不会改变原始 Query 对象。来自 NDB Queries(强调我的):
Instead of specifying an entire query filter in a single expression,
you may find it more convenient to build it up in steps: for example:
query1 = Account.query() # Retrieve all Account entitites
query2 = query1.filter(Account.userid >= 40) # Filter on userid >= 40
query3 = query2.filter(Account.userid < 50) # Filter on userid < 50 too
query3
is equivalent to the query
variable from the previous
example. Note that query objects are immutable, so the
construction of query2
does not affect query1
and the construction
of query3
does not affect query1
or query2
.
来自远程的示例 api shell:
>>>> from models.notification import Notification
>>>> query = Notification.query()
>>>> query
Query(kind='Notification')
>>>>
>>>> # this returns a new ordered query
>>>> query.order(Notification.created_on)
Query(kind='Notification', orders=...)
>>>>
>>>> # but it does not alter the original query
>>>> query
Query(kind='Notification')
>>>>
>>>> # this is how you get the ordered query in a variable
>>>> ordered_query = query.order(Notification.created_on)
>>>> ordered_query
Query(kind='Notification', orders=...)
>>>> query
Query(kind='Notification')
因此请更改您的代码以改用此代码:
q = q.order(-sort_prop)
我尝试在 model.py 中使用这些模型获取最后修改的条目:
class Example(ndb.Model):
....
modified = ndb.DateTimeProperty(auto_now=True)
created = ndb.DateTimeProperty(auto_now_add=True)
查询代码:
for each in ['Example', '...']
model_class = webapp2.import_string('model.%s' % each)
q = model_class.query()
q.order(-model_class.modified)
last_modified_entity = q.get() # does not sort the entities
我也试过:
for each in ['Example', '...']
model_class = webapp2.import_string('model.%s' % each)
q = model_class.query()
sort_prop = ndb.GenericProperty('modified')
q.order(-sort_prop)
last_modified_entity = q.get() # does not sort the entities
在 Query 对象上调用 .order()
不会改变 Query 对象。它 returns 一个带有该订单的新 Query 对象,但不会改变原始 Query 对象。来自 NDB Queries(强调我的):
Instead of specifying an entire query filter in a single expression, you may find it more convenient to build it up in steps: for example:
query1 = Account.query() # Retrieve all Account entitites query2 = query1.filter(Account.userid >= 40) # Filter on userid >= 40 query3 = query2.filter(Account.userid < 50) # Filter on userid < 50 too
query3
is equivalent to thequery
variable from the previous example. Note that query objects are immutable, so the construction ofquery2
does not affectquery1
and the construction ofquery3
does not affectquery1
orquery2
.
来自远程的示例 api shell:
>>>> from models.notification import Notification
>>>> query = Notification.query()
>>>> query
Query(kind='Notification')
>>>>
>>>> # this returns a new ordered query
>>>> query.order(Notification.created_on)
Query(kind='Notification', orders=...)
>>>>
>>>> # but it does not alter the original query
>>>> query
Query(kind='Notification')
>>>>
>>>> # this is how you get the ordered query in a variable
>>>> ordered_query = query.order(Notification.created_on)
>>>> ordered_query
Query(kind='Notification', orders=...)
>>>> query
Query(kind='Notification')
因此请更改您的代码以改用此代码:
q = q.order(-sort_prop)