gpt4 book ai didi

python - ndb.DateTimeProperty 上的 Query.order 没有以正确的顺序返回结果

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:34 26 4
gpt4 key购买 nike

我有以下内容:

class Notification(ndb.Model):
targetUser = ndb.KeyProperty()
pointRoot = ndb.KeyProperty()
followReason = ndb.StringProperty()
notificationReason = ndb.StringProperty()
sourceUser = ndb.KeyProperty() # User who caused notification to be raised
raisedDate = ndb.DateTimeProperty(auto_now_add=True)
cleared = ndb.BooleanProperty(default=False)
clearedDate = ndb.DateTimeProperty(default=None)

@classmethod
def getActiveNotificationsForUser(cls, userKey):
q = cls.query(ndb.AND(cls.targetUser == userKey, cls.cleared == False))
q.order(-cls.raisedDate)
notifications = q.fetch(10)
return notifications

我认为这应该可行,因为它不使用不等式过滤器,仅使用等式过滤器。当它不起作用时,我添加了一个索引,在 index.yaml 中定义如下:

- kind: Notification
properties:
- name: targetUser
- name: cleared
- name: raisedDate
direction: desc

但是,getActiveNotificationsForUser 仍在乱序返回通知。
有谁知道为什么或我能做些什么来解决这个问题(除了获取所有通知并在 python 中排序)?

提前致谢

  • 亚伦

最佳答案

与旧的 db 库不同,ndb.query 对象是不可变的。因此,向现有查询添加过滤器或排序顺序将始终返回新的,而旧的保持不变。你应该这样做:

q = q.order(-cls.raisedDate) 

关于python - ndb.DateTimeProperty 上的 Query.order 没有以正确的顺序返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18903748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com