在 Pyramids/Python/SQLAlchemy 中引用相关对象
我不知道如何标题这个问题。我还简化了我的代码,以便更容易提问。假设我在 Pyramid 的 myproject.models 中有以下代码:
class Links(Base):
__tablename__ = 'links'
id = Column(Integer, primary_key=True)
link = Column(Text)
def __init__(self, link):
self.link = link
class Submissions(Base):
__tablename__ = 'submissions'
id = Column(Integer, primary_key=True)
title = Column(Text)
link_id = Column(Integer, ForeignKey('links.id'))
link = relationship(Links)
def __init__(self, title, link):
self.title = title
self.link = link
视图将非常简单:
def my_view(request):
dbsession = DBSession()
submissions = dbsession.query(Submissions)
return {'submissions':submissions}
我想使用 Chameleon 在我的页面上返回此代码:
<p tal:repeat="thing submissions">
${thing.title} ${thing.link}
</p>
但是, ${thing.link} 不显示该站点的链接。
问题:
- 如何引用 thing.link 的链接?直觉上,我会输入 ${thing.link.link},但这不起作用。
- 如何引用任意子类?我希望能够从对象的子类中提取任何属性,例如 thing.link.link、thing.link.domain、thing.link.created 等。
顺便说一句,有人请告诉我一个更好的标题来给出这个问题。
I'm not sure how to title this question. I've also simplified my code so it's easier to ask. Say I have the following code in myproject.models in Pyramid:
class Links(Base):
__tablename__ = 'links'
id = Column(Integer, primary_key=True)
link = Column(Text)
def __init__(self, link):
self.link = link
class Submissions(Base):
__tablename__ = 'submissions'
id = Column(Integer, primary_key=True)
title = Column(Text)
link_id = Column(Integer, ForeignKey('links.id'))
link = relationship(Links)
def __init__(self, title, link):
self.title = title
self.link = link
The view will be very simple:
def my_view(request):
dbsession = DBSession()
submissions = dbsession.query(Submissions)
return {'submissions':submissions}
I want to return this on my page using Chameleon:
<p tal:repeat="thing submissions">
${thing.title} ${thing.link}
</p>
However, ${thing.link} doesn't show the link of the site.
Questions:
- How do I reference thing.link's link? Intuitively, I would type ${thing.link.link}, but that doesn't work.
- How do I reference an arbitrary subclass? I want to be able to extract any attribute from an object's subclass, for example, thing.link.link, thing.link.domain, thing.link.created, etc.
BTW, someone please tell me a better title to give this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的示例中,您在
.query()
之后缺少.all()
。您可以通过执行类似的操作来检查您的视图是否确实加载了您的提交,然后在加载页面时观察控制台。
然后,当您确认确实已加载它们时,您可以使用
submission.link
访问链接对象。在链接对象中,您可以使用.link
访问链接属性。因此,在您的模板中,您可以编写
${thing.link.link}
。In your example, you are missing the
.all()
after your.query()
. You can check in your view if your submissions are really loaded by doing something likeand then watch your console when loading the page.
Then, when you confirmed you really have them loaded, you can access the link object with
submission.link
. In the link object, you can access the link attribute with.link
.So in your template, you could write
${thing.link.link}
.假设您确实附加了
link
对象(鉴于 <code>link_id 列不可可为空
),很可能您需要(急切)将关系加载到
链接
,因为当您填充视图时会话已经关闭。有关详细信息,请参阅关系加载技术。下面的代码应该可以做到这一点:
Assuming you do have the
link
object attached (given the fact that thelink_id
column is notnullable
), most probably you need to(eager)load
the relationship toLinks
because the session is alread closed when you populate your view.See Relationship Loading Techniques for more information. The code below should do it: