【问题标题】:TypeError: bad operand type for unary -: 'DateTimeProperty'TypeError:一元操作数类型错误-:'DateTimeProperty'
【发布时间】:2013-02-25 22:19:01
【问题描述】:

我正在使用带有 python 2.7 的 Google App Engine 并尝试重现 https://github.com/GoogleCloudPlatform/appengine-paging-python/blob/master/suggest_cursor.py 给出的示例以在我的页面内使用光标分页(请参阅 https://developers.google.com/appengine/articles/paging

数据模型.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'

知道为什么这不能按预期工作吗?

【问题讨论】:

    标签: python google-app-engine pagination cursor data-modeling


    【解决方案1】:

    ndbdb是完全不同的模块;您不能使用 ndb 示例代码并期望它在 db 中工作。

    您需要为.order() 调用提供引用的属性名称:

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

    【讨论】:

    • 啊,谢谢,因为没有数据模型,我不知道这些示例是基于 ndb 的。甚至不知道ndb!谢谢
    猜你喜欢
    • 2013-08-11
    • 2013-03-28
    • 2019-02-17
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多