简体   繁体   English

NDB按StructuredProperty的属性排序

[英]NDB ordering by property of StructuredProperty

Say I have an ndb.Model class that I want to use as a StructuredProperty on another model class: 假设我有一个ndb.Model类,我想在另一个模型类上用作StructuredProperty

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. 我在文档中找不到一个示例,由于我努力从旧API重构到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 = 5),CommonExtraData(count = 1)]而另一个具有[CommonExtraData(count = 7),CommonExtraData(count = 2)],则它将如下排序:

[CommonExtraData(count=7), CommonExtraData(count=2)] [CommonExtraData(count=5), CommonExtraData(count=1)] [CommonExtraData(count = 7),CommonExtraData(count = 2)] [CommonExtraData(count = 5),CommonExtraData(count = 1)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM