NDB 在重复的 Expando StructuredProperty 中查询 GenericProperty [英] NDB querying a GenericProperty in repeated Expando StructuredProperty

查看:25
本文介绍了NDB 在重复的 Expando StructuredProperty 中查询 GenericProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我想弄清楚如何为以下案例构建我的查询

Hey guys im trying to figure out how to structure my query for the following case

首先我定义了一个模型

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", ]

感谢您的帮助

推荐答案

使用 GQL 很容易:

It's easy using GQL:

Item.gql("WHERE variants.dynamic = 'a'").fetch()

这也有效:

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?

更新:

同样的事情适用于 GenericProperty() 或任何其他属性子类.

The same thing works with GenericProperty(), or any other Property subclass.

GenericProperty('variants.dynamic') 被禁止的原因是为了防止人们进行这样的黑客攻击:

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.

也许我们可以向 Property 添加一个标志,跳过此检查,但随后不允许在模型定义中使用该属性(它只允许在查询中使用).

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()

(仅当变体是 Expando 时).

(only if variants is an Expando).

这篇关于NDB 在重复的 Expando StructuredProperty 中查询 GenericProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆