NDB ordering by property of StructuredProperty

ⅰ亾dé卋堺 提交于 2019-12-11 06:35: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:


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



来源:https://stackoverflow.com/questions/9966098/ndb-ordering-by-property-of-structuredproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!