数据存储中的一对多关系

发布于 2024-10-04 22:21:57 字数 2622 浏览 1 评论 0原文

Rafe 在此处对数据存储区中的一对多关系进行了很好的解释卡普兰。我尝试将其适应 UserVenue 的更简单情况。所以用户可以去很多餐馆;我想打印用户电子邮件和用户去过的餐馆:

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]")

并显示与该用户关联的所有场所。

目前尚不清楚UserVenue如何连接。

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

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

发布评论

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

评论(1

烂柯人 2024-10-11 22:21:57

userEmail 不是 for 循环范围内定义的名称。您需要的是:

for v in scott.venues:
    self.response.out.write(scott.userEmail)
    self.response.out.write(v.venue)

编辑:关于您的编辑 - 如果您的目标是进行连接查询 - 您不能,不能使用谷歌应用程序数据存储。但是,您可以像这样获取用户:

query = User.all()
query.filter("userEmail =", "[email protected]")
results=query.fetch(1)
scott=results[0]

然后获取像 scott.venues 这样的场地。该关系在 db.ReferenceProperty 行中定义,它定义了 someuser.venuessomevenue.user

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 =", "[email protected]")
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.

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