![](/img/trans.png)
[英]ndb returning a StructuredProperty subproperty by querying for a StructuredProperty
[英]NDB querying a GenericProperty in repeated Expando StructuredProperty
大家好,我正在尝试找出以下情况的查询结构
首先,我定义了一个模型
class Variant(ndb.Expando):
test = ndb.StringProperty()
class Item(ndb.Model):
test2 = ndb.StringProperty()
variants = ndb.StructuredProperty(Variant, repeated=True)
variant = Variant(test="test", dynamic="a")
item = Item(test2="test", variants=[variant, ])
item.put()
然后查询的东西..到目前为止,我已经尝试过
dynamic = "dynamic"
Item.query(ndb.GenericProperty("variants.%s" % dynamic) == "a")
Item.query(Item._properties["variants.%s" % dynamic] == "a")
Item.query(getattr(Item.variants, dynamic) == "a")
Item.query(getattr(Item, "variants.%s" % dynamic) == "a")
Item.query(ndb.query.FilterNode("variants.%s" % dynamic, "=", "a"))
generic_prop = ndb.GenericProperty()
generic_prop._name = "variants.%s" % dynamic
Item.query(generic_prop == "a")
而且这些都不起作用。.由于数据存储区中的属性名称为
variants.dynamic = ["a", ]
谢谢您的帮助
使用GQL很容易:
Item.gql("WHERE variants.dynamic = 'a'").fetch()
同样可行:
s = StringProperty()
s._name = 'variants.dynamic')
Item.query(s == 'a').fetch()
请提交功能请求; 但是这将是一个平衡的行为。 您想使用什么语法?
更新:
GenericProperty()或任何其他Property子类也可以使用相同的方法。
禁止GenericProperty('variants.dynamic')的原因是为了防止人们进行此类黑客攻击:
class MyHack(ndb.Model):
foo = StringProperty('bar.baz')
这会混淆序列化和反序列化代码。
也许我们可以向Property添加一个标志,以跳过此检查,但随后不允许在模型定义中使用该属性(仅允许在查询中使用该属性)。
或者,也许我们可以完成这项工作(不过,我认为这很难):
Item.query(Item.variants.dynamic == 'a').fetch()
(仅当变体是Expando时)。
您可以通过一点魔术来做到这一点。
简短答案 :
variants_dynamic_property = ndb.GenericProperty()
variants_dynamic_property._name = 'variants.dynamic'
q = Item.query(variants_dynamic_property == 'a')
长答案 :
由于您要查询GenericProperty
,因此需要创建一个作为docs状态,例如:
FlexEmployee.query(ndb.GenericProperty('location') == 'SF')
同样,在查询StucturedProperty
, 文档说明您可以使用该属性的属性,例如:
Contact.query(Contact.address.city == 'Amsterdam')
所以结合这些,您将需要
Item.query(ndb.GenericProperty('variants.dynamic') == 'a')
但是尝试通过ndb.GenericProperty('variants.dynamic')
构造属性会导致以下异常:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/utils.py", line 136, in positional_wrapper
return wrapped(*args, **kwds)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 2366, in __init__
super(GenericProperty, self).__init__(name=name, **kwds)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/utils.py", line 136, in positional_wrapper
return wrapped(*args, **kwds)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 765, in __init__
raise ValueError('Name %r cannot contain period characters' % (name,))
ValueError: Name 'variants.dynamic' cannot contain period characters
但是,您可以通过使用不带属性名称的构造函数来解决此问题,然后在事实之后设置名称:
variants_dynamic_property = ndb.GenericProperty()
variants_dynamic_property._name = 'variants.dynamic'
骇客解决方案 :
临时解决方案:在本例中,使用ComputedProperty
进行查询-
(在模型定义中添加以下内容)
computed_prop = ndb.ComputedProperty(lambda self: self.repeating_prop[0].sub_prop if self.repeating_prop else None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.