如何使用 ReferenceProperty 访问模型中的项目?
这是我之前的问题的后续问题。
我使用 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
的表。链接表是Venue
和Comment
。如何访问 Venue
或 Comment
中的项目?谢谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
venues
实际上是一个查询对象。因此,您需要获取或迭代它。尝试用循环替换 self.response.out.write(result.venues) 行:
但是,这不会很好地扩展。如果有 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: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.