【问题标题】:Where does 'article_set' come from in Django?Django 中的“article_set”从何而来?
【发布时间】:2010-02-20 22:01:12
【问题描述】:

我目前正在开发一个 Django/Pinax 应用程序(我确定我的问题不是 Pinax 特定的,这就是为什么主题标题中没有提到 Pinax),我正在尝试弄清楚整个框架是如何工作的.现在,我必须编写一个视图来将数据传递给模板。我只在 django 手册中看到过:在手册中,ObjectName.objects.all() 只是简单地传递给 render_to_response()。我的任务有点复杂,所以我试图了解它是如何在 pinax 应用程序“配置文件”中完成的,但完全失败了。

所以,profile.html 模板文件有这样一行:

{% for article in other_user.article_set.all %}

'other_user' 是在views.py 中传递给render_to_response() 的'User' 类的一个实例。好的,但是 article_set 不是它的类变量。我未能在应用程序代码中的任何地方找到 article_set 描述,但发现出现了更多类似调用形式的内容:

{% for bookmark_instance in other_user.saved_bookmarks.all.select_related %}

搜索 Django 文档只得到this 页面,不知道“article_set”到底是什么。

什么是article_set?它在哪里定义,它是如何工作的?任何答案或只是文档链接都非常感谢。谢谢。

【问题讨论】:

    标签: django templates django-models pinax


    【解决方案1】:
    【解决方案2】:

    它是由 ForeignKeyManyToManyFieldArticle 模型创建的 artificial field

    【讨论】:

      猜你喜欢
      • 2011-01-09
      • 2019-12-18
      • 2020-06-12
      • 2020-07-24
      • 2016-02-24
      • 2019-01-16
      • 1970-01-01
      • 2021-01-23
      • 2012-07-08
      相关资源
      最近更新 更多