如何排序db.ReferenceProperty属性?

时间:2012-05-06 06:03:43

标签: google-app-engine google-cloud-datastore gql

我有两种这样的:

class A(db.Model):
    propertyA = db.XxxProperty(required=True)

class B(db.Model):
    reference = db.ReferenceProperty(A,collection_name="Bs",required=True)
    date = db.DateTimeProperty(auto_now_add=True)

现在我想要让A.Bs订购,你知道,这意味着使用B.date订购A.Bs. 我怎样才能做到这一点?我应该写什么GQL查询?

谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个

a.Bs.order("date")

或(降序):

a.Bs.order("-date")

答案 1 :(得分:0)

Shay的建议简明扼要,但我认为添加更多细节会有所帮助。

使用与问题相同的两个类示例,我创建了以下类来呈现我的页面(GAE,python,jinja2)

class MainPage(Handler):
    def get(self):
        a_id = '1001'  # magically get id of target entity a somehow
        # get key using a_id, then retrieve entity using that
        a = db.get(db.Key.from_path('A', a_id, parent=reg_key()))
        # look up the collection and assign it to b
        b = a.Bs
        # sort items in collection in reverse
        b.order('-created')
        # pass b to the template to get rendered (I use a custom method in my Handler)
        self.render('main.html', b = b)