Python NDB:属性 查询验证?
Python NDB: Property validation in queries?
我有一个模型:
def category_path_validator(prop , val):
# makes sure the path is of the form `first.second(...)last`
# and that it represents a valid path in the category tree.
pass
class Product(ndb.Model):
category_path = nsb.StringProperty(validator=category_path_validator)
我希望能够获得一个类别中的所有 Products
,所以我的查询是:
Product.Query(ndb.AND(Product.category_path >= 'furniture' , Product.category_path <= 'furniturez'))
(添加 z
有效,因为 <=,>=
比较字符串词典)
现在这会产生一个错误,因为 furniturez
不是一个有效的类别。
有没有办法 query
值 没有 validation
,但仍然能够设置 属性 和 validation
?
我认为最好的方法是将验证分为两部分。第一部分是简化您的验证器以仅检查 StringProperty
:
def category_path_validator(prop , val):
# makes sure the path is of the form `first.second(...)last`
pass
第二部分是加一个Model hook:
def _pre_put_hook(self):
# makes sure that category_path represents a valid path in the category tree.
pass
缺点是你不会知道 category_path
是否坏,直到你尝试 put()
它。
我有一个模型:
def category_path_validator(prop , val):
# makes sure the path is of the form `first.second(...)last`
# and that it represents a valid path in the category tree.
pass
class Product(ndb.Model):
category_path = nsb.StringProperty(validator=category_path_validator)
我希望能够获得一个类别中的所有 Products
,所以我的查询是:
Product.Query(ndb.AND(Product.category_path >= 'furniture' , Product.category_path <= 'furniturez'))
(添加 z
有效,因为 <=,>=
比较字符串词典)
现在这会产生一个错误,因为 furniturez
不是一个有效的类别。
有没有办法 query
值 没有 validation
,但仍然能够设置 属性 和 validation
?
我认为最好的方法是将验证分为两部分。第一部分是简化您的验证器以仅检查 StringProperty
:
def category_path_validator(prop , val):
# makes sure the path is of the form `first.second(...)last`
pass
第二部分是加一个Model hook:
def _pre_put_hook(self):
# makes sure that category_path represents a valid path in the category tree.
pass
缺点是你不会知道 category_path
是否坏,直到你尝试 put()
它。