如何使用 ReferenceProperty 访问模型中的项目?

发布于 2024-10-05 14:25:38 字数 2063 浏览 6 评论 0原文

这是我之前的问题的后续问题。

我使用 ReferenceProperty 设置模型:

class User(db.Model):
    userEmail = db.StringProperty()

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

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

在数据存储中,我在用户下有此条目:

Entity Kind: User
Entity Key: ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBUM
ID: 21
userEmail: [email protected]

在地点下:

Entity Kind: Venue
Entity Key: ag1oZWxsby0xLXdvcmxkcgsLEgVWZW51ZRhVDA
ID: 85
venue (string): 5 star venue
user (Key): ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBUM
User: id=21

我试图像这样在 hw.py 中显示该项目此

query = User.all()
    results = query.fetch(10)
    self.response.out.write("<html><body><ol>")
    for result in results:
        self.response.out.write("<li>")
        self.response.out.write(result.userEmail)
        self.response.out.write(result.venues)
        self.response.out.write("</li>")
    self.response.out.write("</ol></body></html>")

行有效:

        self.response.out.write(result.userEmail)

但此行不工作:

        self.response.out.write(result.venues)

但正如根据vonPetrushev的回答 result.venues 应该获取与此 userEmail 关联的 venue

抱歉,如果这令人困惑:我只是尝试使用 ReferenceProperty 访问链接到 userEmail 的表。链接表是VenueComment。如何访问 VenueComment 中的项目?谢谢。

This is a follow up on my previous question.

I set up the models with ReferenceProperty:

class User(db.Model):
    userEmail = db.StringProperty()

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

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

In datastore I have this entry under User:

Entity Kind: User
Entity Key: ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBUM
ID: 21
userEmail: [email protected]

And under Venue:

Entity Kind: Venue
Entity Key: ag1oZWxsby0xLXdvcmxkcgsLEgVWZW51ZRhVDA
ID: 85
venue (string): 5 star venue
user (Key): ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBUM
User: id=21

I am trying to display the item in hw.py like this

query = User.all()
    results = query.fetch(10)
    self.response.out.write("<html><body><ol>")
    for result in results:
        self.response.out.write("<li>")
        self.response.out.write(result.userEmail)
        self.response.out.write(result.venues)
        self.response.out.write("</li>")
    self.response.out.write("</ol></body></html>")

This line works:

        self.response.out.write(result.userEmail)

But this line does not work:

        self.response.out.write(result.venues)

But as per vonPetrushev's answer result.venues should grab the venue associated with this userEmail.

Sorry if this is confusing: I am just trying to access the tables linked to the userEmail with ReferenceProperty. The linked tables are Venue and Comment. How do I access an item in Venue or in Comment? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

緦唸λ蓇 2024-10-12 14:25:38

venues 实际上是一个查询对象。因此,您需要获取或迭代它。

尝试用循环替换 self.response.out.write(result.venues) 行:

query = User.all()
users = query.fetch(10)
self.response.out.write("<html><body><ol>")
for user in users:
    self.response.out.write("<li>")
    self.response.out.write(user.userEmail)
    self.response.out.write("<ul>")
    # Iterate over the venues
    for venue in user.venues:
        self.response.out.write("<li>%s</li>" % venue.venue)
    self.response.out.write("</ul></li>")
self.response.out.write("</ol></body></html>")

但是,这不会很好地扩展。如果有 10 个用户,它将运行 11 个查询!确保您使用 Appstats 来查找此类性能问题。尽量减少 RPC 调用的次数。

更好的解决方案可能是非规范化并将用户的电子邮件存储在 Venue 类型上。这样,您只需要一次查询即可打印场地信息和用户电子邮件。

venues is actually a query object. So you'll need to fetch or iterate over it.

Try replacing the self.response.out.write(result.venues) line with a loop:

query = User.all()
users = query.fetch(10)
self.response.out.write("<html><body><ol>")
for user in users:
    self.response.out.write("<li>")
    self.response.out.write(user.userEmail)
    self.response.out.write("<ul>")
    # Iterate over the venues
    for venue in user.venues:
        self.response.out.write("<li>%s</li>" % venue.venue)
    self.response.out.write("</ul></li>")
self.response.out.write("</ol></body></html>")

However, this will not scale very well. If there are 10 users, it will run 11 queries! Make sure you are using Appstats to look for performance issues like these. Try to minimize the number of RPC calls you make.

A better solution might be to denormalize and store the user's email on the Venue kind. That way you will only need one query to print the venue information and users email.

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