数据存储中的一对多关系
Rafe 在此处对数据存储区中的一对多关系进行了很好的解释卡普兰。我尝试将其适应 User
和 Venue
的更简单情况。所以用户可以去很多餐馆;我想打印用户电子邮件和用户去过的餐馆:
class User(db.Model):
userEmail = db.StringProperty()
class Venue(db.Model):
user = db.ReferenceProperty(User,
collection_name="venues")
venue = db.StringProperty()
class OneToMany(webapp.RequestHandler):
def get(self):
scott = User(userEmail="[email protected]")
scott.put()
Venue(user=scott,
venue="club1").put()
Venue(user=scott,
venue="club2").put()
for v in scott.venues:
self.response.out.write(v.venue)
这会打印 "club1club2"
但我想关联 "[email protected]"
以及他去过的地方;所以我想打印类似的内容: "[email protected] :club1,club2"
尝试
for v in scott.venues:
self.response.out.write(userEmail)
self.response.out.write(v.venue)
给出错误:
self.response.out.write(userEmail)
NameError: global name 'userEmail' is not defined
正确的方法是什么?谢谢!
EDIT2 (回复:vonPetrushev 的回答)
这似乎有效:
query = User.all()
query.filter("userEmail =", "[email protected]")
results=query.fetch(1)
scott=results[0]
self.response.out.write("%s went to:" % scott.userEmail)
for venue in scott.venues:
self.response.out.write(venue.venue)
显示:
[电子邮件受保护]转到:club1club22
编辑(回复:vonPetrushev的回答)
我有点困惑。
所以我想编写一个这样的查询
query = User.all()
query.filter("userEmail =", "[email protected]")
并显示与该用户关联的所有场所。
目前尚不清楚User
和Venue
如何连接。
There is a nice explanation of 1-to-many relationships in datastore here by Rafe Kaplan. I tried to adapt that to a simpler case of User
and Venue
. So user can go to many restaurants; and I want to print the user email and the restaurants the user went to:
class User(db.Model):
userEmail = db.StringProperty()
class Venue(db.Model):
user = db.ReferenceProperty(User,
collection_name="venues")
venue = db.StringProperty()
class OneToMany(webapp.RequestHandler):
def get(self):
scott = User(userEmail="[email protected]")
scott.put()
Venue(user=scott,
venue="club1").put()
Venue(user=scott,
venue="club2").put()
for v in scott.venues:
self.response.out.write(v.venue)
This prints "club1 club2"
But I want to associate "[email protected]"
with the places he went to; so I want to print something like: "[email protected]: club1, club2"
Trying
for v in scott.venues:
self.response.out.write(userEmail)
self.response.out.write(v.venue)
gives the error:
self.response.out.write(userEmail)
NameError: global name 'userEmail' is not defined
What is the correct way of doing this? Thanks!
EDIT2 (re: answer by vonPetrushev)
This seems to work:
query = User.all()
query.filter("userEmail =", "[email protected]")
results=query.fetch(1)
scott=results[0]
self.response.out.write("%s went to:" % scott.userEmail)
for venue in scott.venues:
self.response.out.write(venue.venue)
Displays:
[email protected] went to:club1club22
EDIT (re: answer by vonPetrushev)
I am a little bit confused.
So I want to write a query like this
query = User.all()
query.filter("userEmail =", "[email protected]")
and display all the venues associated with this user.
It is not clear how User
and Venue
is connected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
userEmail
不是for
循环范围内定义的名称。您需要的是:编辑:关于您的编辑 - 如果您的目标是进行连接查询 - 您不能,不能使用谷歌应用程序数据存储。但是,您可以像这样获取用户:
然后获取像
scott.venues
这样的场地。该关系在db.ReferenceProperty
行中定义,它定义了someuser.venues
和somevenue.user
。userEmail
is not a defined name in the scope of thefor
loop. What you need is:EDIT: Concerning your edit - if your goal is to make a joined query - you can't, not with the google app datastore. However, you can grab the user like this:
and then get the venues like
scott.venues
. The relation is defined in the line withdb.ReferenceProperty
, which defines what issomeuser.venues
and what issomevenue.user
.