Google App Engine 中的一对多数据库设置

发布于 2024-10-18 05:23:19 字数 861 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以歌曲疗慰 2024-10-25 05:23:19

ReferenceProperty 在引用的实体中创建一个自动查询(如果您提供了一个,则使用 collection_name,您确实这样做了):

sams_bills = Student.all().filter("name =", "Samuel").get().bills

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 = Student.all().filter("name =", "Samuel").get().bills

sams_bills is now a db.Query for the bills, which you can call .fetch() on to retrieve one or more bills.

甜宝宝 2024-10-25 05:23:19

我认为对于有 SQL 经验的人来说,最难理解 App Engine 的一点是,很多东西需要两次查询才能获得您想要的结果。

student = Student.all().filter('name =', 'Samuel').get()
bill = Bill.all().filter('student =', student.key()).get()

对于有 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.

student = Student.all().filter('name =', 'Samuel').get()
bill = Bill.all().filter('student =', student.key()).get()

The second hardest thing for people with SQL experience to grok is that hardly anyone uses GQL. ;)

红焚 2024-10-25 05:23:19

Bill 类中 db.ReferenceProperty“student”的 collection_name 参数已经为您设置了查询。因此,您所要做的就是:

student = Student.all().filter('name =', 'Samuel').get()
for bill in student.bills:
    logging.info('Student %s Month:%s Energy:%d' % (student.name, str(bill.bill_month), bill.energy)

现在反向引用查询返回的结果是无序的。您可以(如果您的索引设置正确)使用 .order() 按特定顺序返回它们,或者您可以将它们放入 Set 中并在内存中对它们进行排序(非常快),如下所示:

sorted_bills = []
for bill in student.bills:
    sorted_bills.append(bill)

# Sort additions by month then by amount (secondary key sorts first in code)
sorted_bills = sorted(sorted_bills, key=lambda Bill: Bill.energy, reverse=True)
sorted_bills = sorted(sorted_bills, key=lambda Bill: Bill.bill_month, reverse=False)

在本例中,如果学生有多个账单具有相同的 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:

student = Student.all().filter('name =', 'Samuel').get()
for bill in student.bills:
    logging.info('Student %s Month:%s Energy:%d' % (student.name, str(bill.bill_month), bill.energy)

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:

sorted_bills = []
for bill in student.bills:
    sorted_bills.append(bill)

# Sort additions by month then by amount (secondary key sorts first in code)
sorted_bills = sorted(sorted_bills, key=lambda Bill: Bill.energy, reverse=True)
sorted_bills = sorted(sorted_bills, key=lambda Bill: Bill.bill_month, reverse=False)

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文