TypeError:一元的坏操作数类型 - :'DateTimeProperty'

时间:2013-02-25 14:14:43

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

我正在使用带有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

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)with removed - 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 个答案:

答案 0 :(得分:1)

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

您需要将引用的媒体资源名称提供给.order()电话:

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