本文介绍了由StructuredProperty的属性进行NDB排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在另一个模型类中使用 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)


$ b

然后我可以做这样的查询:

  MyObject.query()。order(-MyObject.extra.count)

我可以在文档中找不到示例,而我的开发环境目前还不能正常工作,这是因为我努力从旧API重构到NDB。

解决方案刚刚做了一些实验,看起来很有效。你应该能够按照你写出来的方式完成它。



在实验中,我发现了一个应该注意的怪癖 - 如果你使用重复属性,它似乎排序仅限重复财产的第一个实例。例如,如果您的重复属性具有[CommonExtraData(count = 5),CommonExtraData(count = 1)],另一个具有[CommonExtraData(count = 7),CommonExtraData(count = 2)],则它将按如下排序: p>

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

这篇关于由StructuredProperty的属性进行NDB排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:29