数据存储中的一对多关系 [英] One-to-many relationships in datastore
问题描述
数据存储区此处中的一对多关系有一个很好的解释>由Rafe Kaplan提供。我试图将其适用于 User
和 Venue
的简单情况。所以用户可以去许多餐馆;我想打印用户的电子邮件和用户去过的餐厅:
$ $ $ $ $ $ $ $ $ $ codelass User(db.Model):
userEmail = db.StringProperty()
class Venue(db.Model):
user = db.ReferenceProperty(User,
collection_name =venues)
场地= db.StringProperty()
类OneToMany(webapp.RequestHandler):
def get(self):
scott = User(userEmail =scott@example.com )
scott.put()
Venue(user = scott,
venue =club1)。put()
Venue(user = scott,
=club2)。put()
for v in scott.venues:
self.response.out.write(v.venue)
打印club1 club2
但我想将scott@example.com
与他去过的地点联系起来;所以我想打印如下内容:scott@example.com:club1,club2
Trying
for v in scott.venues:
self.response.out.write(userEmail)
self.response .out.write(v.venue)
给出错误:
self.response.out.write(userEmail)
NameError:全局名称'userEmail'未定义
这样做的正确方法是什么?谢谢!
EDIT2 (re:vonPetrushev回答)
work:
query = User.all()
query.filter(userEmail =,scott @ example .com)
results = query.fetch(1)
scott = results [0]
self.response.out.write(%s转到:%scott.userEmail)
在scott.venues中的场地:
self.response.out.write(venue.venue)
显示:
scott@example.com前往:club1club22
编辑(re:由vonPetrushev回答)
我有点困惑。 >
所以我想写一个这样的查询
query = User.all ()
query.filter(userEmail =,scott@example.com)
<显示所有与此用户相关的场所。
目前尚不清楚 User
和 Venue
已连接。
userEmail
在范围内不是定义的名称
循环。你需要的是:
for v in scott.venues:
self.response.out.write(scott。 userEmail)
self.response.out.write(v.venue)
编辑:关于你的编辑 - 如果你的目标是做一个加入的查询 - 你不能,而不是与谷歌应用程序数据存储。但是,您可以像这样获取用户:
query = User.all()
query.filter( userEmail =,scott@example.com)
results = query.fetch(1)
scott = results [0]
然后得到像 scott.venues
这样的场地。关系定义在 db.ReferenceProperty
的行中,它定义了什么是 someuser.venues
,什么是 somevenue.user
。
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="scott@example.com")
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 "scott@example.com"
with the places he went to; so I want to print something like: "scott@example.com: 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 =", "scott@example.com")
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:
scott@example.com 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 =", "scott@example.com")
and display all the venues associated with this user.
It is not clear how User
and Venue
is connected.
userEmail
is not a defined name in the scope of the for
loop. What you need is:
for v in scott.venues:
self.response.out.write(scott.userEmail)
self.response.out.write(v.venue)
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:
query = User.all()
query.filter("userEmail =", "scott@example.com")
results=query.fetch(1)
scott=results[0]
and then get the venues like scott.venues
. The relation is defined in the line with db.ReferenceProperty
, which defines what is someuser.venues
and what is somevenue.user
.
这篇关于数据存储中的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!