在 Pyramids/Python/SQLAlchemy 中引用相关对象

发布于 2024-12-05 17:14:37 字数 1158 浏览 1 评论 0原文

我不知道如何标题这个问题。我还简化了我的代码,以便更容易提问。假设我在 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} 不显示该站点的链接。

问题:

  1. 如何引用 thing.link 的链接?直觉上,我会输入 ${thing.link.link},但这不起作用。
  2. 如何引用任意子类?我希望能够从对象的子类中提取任何属性,例如 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:

  1. How do I reference thing.link's link? Intuitively, I would type ${thing.link.link}, but that doesn't work.
  2. 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 技术交流群。

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

发布评论

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

评论(2

執念 2024-12-12 17:14:37

在您的示例中,您在 .query() 之后缺少 .all() 。您可以通过执行类似的操作来检查您的视图是否确实加载了您的提交

for submission in submissions:
    print submission.id, submission.title

,然后在加载页面时观察控制台。

然后,当您确认确实已加载它们时,您可以使用 submission.link 访问链接对象。在链接对象中,您可以使用 .link 访问链接属性。

for submission in submissions:
    print 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 like

for submission in submissions:
    print submission.id, submission.title

and 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.

for submission in submissions:
    print submission.link.link

So in your template, you could write ${thing.link.link}.

遮云壑 2024-12-12 17:14:37

假设您确实附加了 link 对象(鉴于 <​​code>link_id 列不可 可为空),很可能您需要 (急切)将关系加载到链接,因为当您填充视图时会话已经关闭。
有关详细信息,请参阅关系加载技术。下面的代码应该可以做到这一点:

submissions = dbsession.query(Submissions).options(joinedload('link'))

Assuming you do have the link object attached (given the fact that the link_id column is not nullable), most probably you need to (eager)load the relationship to Links because the session is alread closed when you populate your view.
See Relationship Loading Techniques for more information. The code below should do it:

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