问题描述
嘿家伙即时尝试弄清楚如何构建我的查询为以下情况
首先我有一个模型定义
class Variant(ndb.Expando):
test = ndb.StringProperty()
class Item(ndb。模型):
test2 = ndb.StringProperty()
variants = ndb.StructuredProperty(Variant,repeated = True)
$ b $ 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()
也可以这样工作:
pre code $ s = StringProperty(
s._name ='variants.dynamic')
Item.query(s =='a')。fetch()
请求;然而这将是一个平衡的行为。你想用什么语法?
更新:
可以使用GenericProperty()或其他任何Property子类。
$ b
GenericProperty('variants.dynamic')被禁止的原因是为了防止人们这样做:
class MyHack(ndb.Model):
foo = StringProperty('bar.baz')
这会混淆序列化和反序列化代码。
也许我们可以为属性添加一个标志,以跳过此检查,但不允许在模型定义中使用该属性(它只允许在查询中使用该属性)。
或者,也许我们可以使这个工作(我认为这将是艰难的,虽然):
Item.query(Item.variants.dynamic ==' a)。fetch()
(仅当变体是Expando时)。
Hey guys im trying to figure out how to structure my query for the following case
First i have a model defined
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()
and then for the query stuff.. So far i've tried
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")
and none of these works.. This should be perfectly possible since the property name in the datastore is
variants.dynamic = ["a", ]
Thank you for your help
It's easy using GQL:
Item.gql("WHERE variants.dynamic = 'a'").fetch()
Also this works:
s = StringProperty() s._name = 'variants.dynamic') Item.query(s == 'a').fetch()
Please do file a feature request; however it's going to be a balancing act. What syntax would you like to use?
UPDATE:
The same thing works with GenericProperty(), or any other Property subclass.
The reason that GenericProperty('variants.dynamic') is forbidden is to prevent people from doing hacks like this:
class MyHack(ndb.Model): foo = StringProperty('bar.baz')
which will confuse the serialization and deserialization code.
Maybe we can add a flag to Property that skips this check but then disallows using the property in a model definition (it would only allow it in a query).
Or maybe we can make this work (I think this would be hard though):
Item.query(Item.variants.dynamic == 'a').fetch()
(only if variants is an Expando).
这篇关于NDB在重复的Expando StructuredProperty中查询GenericProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!