Google App Engine 中的一对多数据库设置
class Student(db.Model):
teacher = db.ReferenceProperty(Teacher, collection_name='students')
name = db.StringProperty(required=True)
zip_code = db.IntegerProperty(required=True)
square_footage = db.IntegerProperty(required=True)
month_entries = db.IntegerProperty(required=True)
class Bill(db.Model):
student = db.ReferenceProperty(Student, collection_name='bills')
bill_month = db.DateProperty(required=True)
energy = db.IntegerProperty(required=True)
从上面显示的模型设置中...我可以使用类似以下内容轻松显示存储的所有账单:
bill = models.Bill.all()
for stubs in bill:
print stubs.energy
print stubs.student.name
但是如何列出每个学生拥有的账单? 在 SQL 中我会这样说:
SELECT * FROM Bill WHERE Student.Name = Samuel
我想我不明白如何检索 ReferenceProperty 给出的账单。 GQL 中似乎没那么简单。如何通过参考属性查询?
class Student(db.Model):
teacher = db.ReferenceProperty(Teacher, collection_name='students')
name = db.StringProperty(required=True)
zip_code = db.IntegerProperty(required=True)
square_footage = db.IntegerProperty(required=True)
month_entries = db.IntegerProperty(required=True)
class Bill(db.Model):
student = db.ReferenceProperty(Student, collection_name='bills')
bill_month = db.DateProperty(required=True)
energy = db.IntegerProperty(required=True)
From my models setup shown above... I can easily show all the Bills stored using something like this:
bill = models.Bill.all()
for stubs in bill:
print stubs.energy
print stubs.student.name
But how do I list what Bills each student has?
In SQL I would say something like:
SELECT * FROM Bill WHERE Student.Name = Samuel
I guess I don't understand how to retrieve the Bills given by ReferenceProperty. It doesn't seem so simple in GQL. How do I query by Reference Property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ReferenceProperty 在引用的实体中创建一个自动查询(如果您提供了一个,则使用 collection_name,您确实这样做了):
sams_bills
现在是帐单的 db.Query,您可以调用 .fetch()来检索一张或多张钞票。The ReferenceProperty creates an automatic query in the referenced entity (using the collection_name if you provided one, which you did):
sams_bills
is now a db.Query for the bills, which you can call .fetch() on to retrieve one or more bills.我认为对于有 SQL 经验的人来说,最难理解 App Engine 的一点是,很多东西需要两次查询才能获得您想要的结果。
对于有 SQL 经验的人来说,第二个最难理解的事情是几乎没有人使用 GQL。 ;)
I think the hardest thing for people with SQL experience to grok about App Engine is that a lot of stuff requires two queries to get the results you want.
The second hardest thing for people with SQL experience to grok is that hardly anyone uses GQL. ;)
Bill 类中 db.ReferenceProperty“student”的 collection_name 参数已经为您设置了查询。因此,您所要做的就是:
现在反向引用查询返回的结果是无序的。您可以(如果您的索引设置正确)使用 .order() 按特定顺序返回它们,或者您可以将它们放入 Set 中并在内存中对它们进行排序(非常快),如下所示:
在本例中,如果学生有多个账单具有相同的 bill_month 值,最大的账单将首先排序(注意反向= True 参数)。
The collection_name parameter of the db.ReferenceProperty "student" in the Bill class has already set up a query for you. So all you have to do is this:
Now the results returned by the back-referenced query are unordered. You can (if you have your indexes set up right) use .order() to return them in a particular order, or you can put them into a Set and sort them in memory (very fast) like this:
In this example, if a student had more than one bill with the same bill_month value, the largest bills would be sorted first (note the reverse=True parameter).