类型错误:对一元 - 的操作数类型错误:'DateTimeProperty

0 投票
1 回答
1476 浏览
提问于 2025-04-17 17:03

我正在使用 Google App Engine 和 Python 2.7,想要按照这个链接中的例子来实现分页功能,链接是 https://github.com/GoogleCloudPlatform/appengine-paging-python/blob/master/suggest_cursor.py,我想在我的页面里使用光标分页(你可以查看这个链接了解更多 https://developers.google.com/appengine/articles/paging

这是我的 datamodel.py 文件

class Feedback(db.Model):
    user = db.ReferenceProperty(User)
    subject = db.StringProperty()
    text = db.TextProperty(default='')
    created = db.DateTimeProperty(auto_now_add=True)

我的代码看起来很相似,但我遇到了错误:

1)

query = datamodel.Feedback.all().order(-datamodel.Feedback.created)
TypeError: bad operand type for unary -: 'DateTimeProperty'

2) 当我去掉这行 - query = datamodel.Feedback.all().order(datamodel.Feedback.created) 时,结果是这个错误

File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2614, in order
    if property.startswith('-'):
AttributeError: 'DateTimeProperty' object has no attribute 'startswith'

有没有人知道为什么这个不按预期工作呢?

1 个回答

1

ndbdb是完全不同的模块;你不能直接把ndb的示例代码拿来用在db上,它们是不能互换的。

在调用.order()的时候,你需要把属性名称用引号括起来:

query = datamodel.Feedback.all().order('-created')

撰写回答