我有一个模特
from rated.lib import utility, data, money, search, kigo
class Property(ndb.Model):
created_at = data.UTCDateTimeProperty(auto_now_add=True)
updated_at = data.UTCDateTimeProperty(auto_now_add=True, auto_now=True, indexed=False)
users = ndb.KeyProperty(repeated=True)
user = ndb.KeyProperty()
rent_ = money.MoneyStructuredProperty(name='rent')
rent = money.structured_value(rent_)
但当我尝试使用查询 -
按租金获取记录订单时properties = Property.query().order(Property.rent)
它给了我错误 -
TypeError: order() expects a Property or query Order; received <super: <class 'structured_value'>, <structured_value object>>
structured_value是一个类,它的语法是 -
class structured_value(object):
def __init__(self, field):
self.field = field
def __get__(self, instance, owner):
if instance is None:
return super(structured_value, self).__get__(instance, owner)
value = self.field.__get__(instance, owner)
if value is None:
return None
if value.cached is None:
value.cached = Money(value.value, value.currency)
print
return value.cached
def __set__(self, instance, value):
if value is None:
self.field.__set__(instance, value)
return
model_value = MoneyModel(
value=str(value.value),
currency=value.currency
)
model_value.cached = value
self.field.__set__(instance, model_value)
我是ndb的新手,浪费时间来解决这个问题...... 任何帮助将不胜感激
答案 0 :(得分:1)
由于错误表明您没有尝试按property
或Query order
订购。
money.structured_value(rent_)
来电回复了什么?你认为它会如何运作?
根据您的代码rent_
,您应该订购,假设您的MoneyStructuredProperty
是可订购的或订购有意义。
好的,您已经包含了结构化值的代码,而不是如何构建自定义属性,所以当然您无法按顺序排序。
阅读构建结构化属性的内容,对于初学者,他们必须从ndb.Model继承。