NDB ordering by property of StructuredProperty

2019-07-26 23:39发布

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.

1条回答
Explosion°爆炸
2楼-- · 2019-07-27 00:45

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

查看更多
登录 后发表回答