OrderBy是一个ReferenceProperty中的一个属性 [英] GqlQuery OrderBy a property in a ReferenceProperty

查看:134
本文介绍了OrderBy是一个ReferenceProperty中的一个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何通过ReferenceProperty的属性对GqlQuery进行排序。

I don't know how to make a GqlQuery that order by a property of the ReferenceProperty.

在这种情况下,我想得到所有的座位,但是顺序通过房间的号码

In this case, I want to get all the Seat but order by the id of the Room

GqlQuery引用不允许加入,所以在这种情况下如何处理?

The GqlQuery reference does not allow a join, so how do you approach in this case?

class Room(db.Model):
    id = db.IntegerProperty()
    dimension = db.StringProperty(multiline=True)

一个房间有很多座位,每个都有一个id

a room has many seats, each with an id

class Seat(db.Model):
    id = db.IntegerProperty()
    roomId = db.ReferenceProperty(Room)



seats_query = GqlQuery("SELECT * FROM Seat ")
seats = seats_query.fetch(1000)

Alex是正确的,所以我决定不要触摸GqlQuery,而是尝试使用Django模板,我已经找到了解决方案来排序Seat by Room'id而无需添加新的字段在座位课。

Alex is right on his point, so I decided not to touch GqlQuery, and tried to play with Django template instead, I have found the solution to sort Seat by Room'id without having to add new field in Seat class.

如果有兴趣,我将代码放在这里;))Cheers;))

I put the code here if anyone has interests ;)) Cheers ;))

        {% regroup seats|dictsort:"roomId.id" by roomId.id as room_list %}
        <ul>
         {% for room in room_list %}
          <li>Room: {{room.grouper}}
            <ul>
            {% for item in room.list %}
            <li>Seat no: {{item.id}} </li>
            {% endfor %}
            </ul>
          </li>
          {% endfor %}
        </ul>


推荐答案

一般来说,解决非关系数据库缺乏的JOIN功能,你反规范化;具体来说,您冗余地将数据放在多个位置,因此可以在您的查询中有效访问这些数据(这样做会使您的存储更新变得更麻烦,但是非关系数据库通常大量倾斜读取

In general, to work around non-relational DBs lack of JOIN functionality, you denormalize; specifically, you redundantly put data pieces in more than one place, so they can be effectively accessed in your queries (this does make it more cumbersome to update your store, but then, non-relational DBs in general are heavily slanted to read-mostly applications).

在这种情况下,您添加到 Seat 具有房间ID的属性 - 因为您有特别称为 roomId (而不是说, room 参考到座位的房间,我想你会把这个非规范化的位作为 roomIdId

In this case, you add to Seat a property with the room's id -- since you have peculiarly called roomId (rather than, say, room) the reference to the seat's room, I guess you'd have this denormalized bit as roomIdId.

这篇关于OrderBy是一个ReferenceProperty中的一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆