Psionman
Psionman

Reputation: 3677

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. The blogpage has a ForeignKey reference to the listing page.

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.

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

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

{% 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 %}

Upvotes: 0

Views: 449

Answers (1)

Charnel
Charnel

Reputation: 4432

You can access related objects in this way:

item.blogdetailpage_set.all()

Upvotes: 1

Related Questions