Sorting a query by a ListProperty (NDB)

2019-07-03 11:50发布

How do i sort a query by a ListProperty*?

the Model:

class Chapter(ndb.Model):
    title = ndb.StringProperty(required=True)
    version = ndb.IntegerProperty(repeated=True)

the 'version' stores values like:

1.1 -> [1,1]
1 -> [1]
2.1.1.1.1 -> [2,1,1,1,1]
1.2 -> [1,2]
2.1.2 -> [2,1,2]

I want to order it like:

1
1.1
1.2
2.1.1.1.1
2.1.2

*Im using NDB so, ListProperty = ndb.IntegerProperty(repeated=True)

2条回答
劫难
2楼-- · 2019-07-03 12:28

That's not how listproperties work, unfortunately. For an ascending-order query, the value used will be the smallest in the list. You'll have to store the values differently (as a string, for example) to do what you're asking.

查看更多
Rolldiameter
3楼-- · 2019-07-03 12:31

OMG I finally did it.

newChaps = sorted(chaps, key=lambda obj: obj.version)

So simple... and yet so much time to find it...

查看更多
登录 后发表回答