NDB 按 StructuredProperty 的属性排序 [英] NDB ordering by property of StructuredProperty
问题描述
假设我有一个 ndb.Model
类,我想将它用作另一个模型类上的 StructuredProperty
:
class CommonExtraData(ndb.Model):计数 = ndb.IntegerProperty(required=True)类 MyObject(ndb.Model):名称 = ndb.StringProperty(required=True)额外 = ndb.StructuredProperty(CommonExtraData, required=True)
然后我可以做这样的查询吗:
MyObject.query().order(-MyObject.extra.count)
我在文档中找不到示例,并且由于我努力从旧 API 重构为 NDB,我的开发环境目前无法正常工作.
刚刚做了一些实验,它似乎奏效了.你应该能够完全按照你写的去做.
在试验中,我发现了一个应该注意的怪癖 - 如果您使用重复属性,它似乎只对重复属性的第一个实例进行排序.例如,如果您的重复属性有 [CommonExtraData(count=5), CommonExtraData(count=1)] 和另一个有 [CommonExtraData(count=7), CommonExtraData(count=2)],它会这样排序:>
[CommonExtraData(count=7), CommonExtraData(count=2)][CommonExtraData(count=5), CommonExtraData(count=1)]
Say I have an ndb.Model
class that I want to use as a StructuredProperty
on another model class:
class CommonExtraData(ndb.Model):
count = ndb.IntegerProperty(required=True)
class MyObject(ndb.Model):
name = ndb.StringProperty(required=True)
extra = ndb.StructuredProperty(CommonExtraData, required=True)
Could I then do a query like this:
MyObject.query().order(-MyObject.extra.count)
I can't find an example in the docs, and my dev environment is not currently working due to my endeavors of refactoring from the old API to NDB.
Just did a bit of experimentation and it appears to be working. You should be able to do it exact as you wrote out.
In experimenting, I found one quirk that should be noted - if you use repeated property, it appears to sort on the first instance of the repeated property only. For example, if your repeated property had [CommonExtraData(count=5), CommonExtraData(count=1)] and another had [CommonExtraData(count=7), CommonExtraData(count=2)], it would sort like this:
[CommonExtraData(count=7), CommonExtraData(count=2)] [CommonExtraData(count=5), CommonExtraData(count=1)]
这篇关于NDB 按 StructuredProperty 的属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!