简体   繁体   English

如何从 django 查询集中找到孩子

[英]How to find the children from a django queryset

I have two django models One for blog pages and one for the blog listing: a list of all blogs.我有两个 Django 模型,一个用于博客页面,一个用于博客列表:所有博客的列表。 The blogpage has a ForeignKey reference to the listing page.博客页面有一个对列表页面的 ForeignKey 引用。

class BlogListingPage(Page):
...
class BlogDetailPage(Page):

    blog_listing = models.ForeignKey(BlogListingPage,
                                     on_delete=models.PROTECT,
                                     blank=True,
                                     null=True,
                                     related_name='+',)

In views.py I have tried to look at the queryset object, but I cannot find a refernce to the detail pages.在 views.py 中,我尝试查看 queryset 对象,但找不到对详细信息页面的引用。

def blog(request):
    context = {'data': BlogListingPage.objects.all()}
    query_set = context['data']
    for item in query_set:
        print(item.__dict__)

It does correctly tell me the number of detail pages in numchild它确实正确地告诉我numchild 中的详细信息页数

How can I access the children themselves?我如何自己访问孩子?

[EDIT] I have looked at the answer to this question but it doesn't tell us how to generate event_set [编辑] 我看过这个问题的答案,但它没有告诉我们如何生成event_set

{% for blog_listing in data %}
    <h2>{{ blog_listing.blog_listing_title }}</h2>
    {% for post in blog_listing.blogdetailpage %}
        <a href="{% pageurl post %}">{{ post.blog_title }}</a>
    {% endfor %}
{% endfor %}

您可以通过这种方式访问相关对象:

item.blogdetailpage_set.all()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM