更新时间:2023-02-18 23:37:15
Note that list.bb
will only give you the RelatedManager
. Here an instance of A
can be related to multiple instances of B
.
因此,要获取所有内容,您需要使用以下语法:
So to get them all you need to use following syntax:
{% for a_obj in mylist %}
{% for b_obj in a_obj.bb.all %}
{{ b_obj }}
{% endfor %}
{% endfor %}
提供了更多详细信息此处 :
More details provided here:
您可以通过在
ForeignKey
定义中设置related_name
参数来覆盖FOO_set
名称.例如,如果将Entry
模型更改为blog = ForeignKey(Blog, on_delete=models.CASCADE, related_name='entries')
,则上面的示例代码将如下所示:
You can override the
FOO_set
name by setting therelated_name
parameter in theForeignKey
definition. For example, if theEntry
model was altered toblog = ForeignKey(Blog, on_delete=models.CASCADE, related_name='entries')
, the above example code would look like this:
>>> b = Blog.objects.get(id=1)
>>> b.entries.all() # Returns all Entry objects related to Blog.