如何在使用ReferenceProperty时查询数据存储? [英] How to query datastore when using ReferenceProperty?

查看:84
本文介绍了如何在使用ReferenceProperty时查询数据存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解数据存储中的一对多关系;但我无法理解当模型包含 ReferenceProperty 时如何查询和更新用户记录。假设我有这样的模型:

$ p $ class User(db.Model):
userEmail = db.StringProperty()
userScore = db.IntegerProperty(default = 0)

class评论(db.Model):
user = db.ReferenceProperty(User,collection_name =comments)
comment = db.StringProperty()

class Venue(db.Model):
user = db.ReferenceProperty(User,collection_name =venues)
venue = db。 StringProperty()

如果我理解正确,同一个用户,由 userEmail 可以有很多评论,并可能与许多场所(餐馆等)相关联。

现在,假设用户 > az@example.com 已经存在于数据库中,他提交了一个新条目。

根据这个答案我做了类似的事情:

  q = User.all()
q.filter(userEm ail =,az@example.com)
results = q.fetch(1)
newEntry = results [0]

但我不清楚这是干什么的!我想要做的是更新类中的 comment 场地字段Comment class Venue



你能帮我理解这是如何工作的吗?感谢。 您发布的代码段正在执行此操作(请参阅评论):

  q = User.all()#准备用于查询
的用户表q.filter(userEmail =,az@example.com)#apply filter ,电子邮件查询
- 这是一个简单的where子句
results = q.fetch(1)#执行查询,应用限制1
the_user = results [0]#结果是一个列表的对象,抓住第一个

这段代码 the_user 将是一个对应于用户记录的对象,其电子邮件地址为az@example.com。如果你已经设置了你的引用属性,你可以通过 the_user.comments the_user.venues 。这些可以修改,例如这样:

  some_venue = the_user.venues [0]#第一个来自列表
some_venue.venue ='在DC。 '
db.put(some_venue)#条目将被更新

我建议您对gae文档进行了一般性的介绍,其中有很好的例子,您会发现它非常有用:
http://code.google.com/appengine/docs/python/overview.html



** UPDATE ** :为了向用户添加新的场地,只需创建新的场地并将查询的用户对象指定为场地的用户属性即可:

  new_venue = Venue(场地='Jeferson memorial',user = the_user)#小心引用
db.put(new_venue)


I am trying to understand the 1-to-many relationships in datastore; but I fail to understand how query and update the record of a user when the model includes ReferenceProperty. Say I have this model:

class User(db.Model):
    userEmail = db.StringProperty()
    userScore = db.IntegerProperty(default=0)

class Comment(db.Model):
    user = db.ReferenceProperty(User, collection_name="comments")
    comment = db.StringProperty()

class Venue(db.Model):
    user = db.ReferenceProperty(User, collection_name="venues")
    venue = db.StringProperty()

If I understand correctly, the same user, uniquely identified by userEmail can have many comments and may be associated with many venues (restaurants etc.)

Now, let's say the user az@example.com is already in the database and he submits a new entry.

Based on this answer I do something like:

q = User.all()
q.filter("userEmail =", az@example.com)
results = q.fetch(1)
newEntry = results[0]

But I am not clear what this does! What I want to do is to update comment and venue fields which are under class Comment and class Venue.

Can you help me understand how this works? Thanks.

解决方案

The snippet you posted is doing this (see comments):

q = User.all() # prepare User table for querying
q.filter("userEmail =", "az@example.com")  # apply filter, email lookup 
                                              - this is a simple where clause
results = q.fetch(1) # execute the query, apply limit 1
the_user = results[0] # the results is a list of objects, grab the first one

After this code the_user will be an object that corresponds to the user record with email "az@example.com". Seing you've set up your reference properties, you can access its comments and venues with the_user.comments and the_user.venues. Some venue of these can be modified, say like this:

some_venue = the_user.venues[0] # the first from the list
some_venue.venue = 'At DC. square'
db.put(some_venue) # the entry will be updated

I suggest that you make a general sweep of the gae documentation that has very good examples, you will find it very helpful: http://code.google.com/appengine/docs/python/overview.html

** UPDATE **: For adding new venue to user, simply create new venue and assign the queried user object as the venue's user attribute:

new_venue = Venue(venue='Jeferson memorial', user=the_user) # careful with the quoting
db.put(new_venue)

这篇关于如何在使用ReferenceProperty时查询数据存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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