0

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 %}
Psionman
  • 3,084
  • 1
  • 32
  • 65
  • Does this answer your question? [Django reverse lookup of foreign keys](https://stackoverflow.com/questions/15306897/django-reverse-lookup-of-foreign-keys) – Henry Woody Jan 24 '20 at 18:06
  • 3
    You are using `'+'` as `related_name` which prevents Django to create a backwards relation. From the [docs](https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.ForeignKey.related_name) – gdef_ Jan 24 '20 at 18:44
  • @HenryWoody That question does not tell us how to generate the event_set, so I am none the wiser – Psionman Jan 25 '20 at 09:13
  • @Psionman `event_set` is the (default) related name for `Event` in relation to `Venue`, you've set the related name as `"+"` here, so you'll get a syntax error if you try the same here. Consider changing the related name (or leaving it out and going with the default), and that same syntax will work. – Henry Woody Jan 25 '20 at 23:05

1 Answers1

1

You can access related objects in this way:

item.blogdetailpage_set.all()
Charnel
  • 4,222
  • 2
  • 16
  • 28
  • where does blogdetailpage_set come from? "AttributeError: 'BlogListingPage' object has no attribute 'blogdetailpage_set' " – Psionman Jan 25 '20 at 09:14
  • @Psionman this should be a backwards relations from `BlogDetailPage` to `BlogListingPage` record, but I see you put `+` as `related_name` on FK in `BlogDetailPage` so this not gona work. Remove that attribute (or change it to a word-like) and create/run the migration - this will work then. – Charnel Jan 25 '20 at 09:52
  • That works thanks. How do I access it in my template? {% for post in blog_listing.blogdetailpage_set.all %} doesn't seem to return anything? – Psionman Jan 25 '20 at 10:13
  • You're welcome. `blog_listing` is a model instance or queryset? In second case you can't access related objects that way. – Charnel Jan 25 '20 at 10:31
  • .RelatedManager'. So what do I do? – Psionman Jan 25 '20 at 10:44
  • so, it's a queryset (or then why you got `RelatedMager` after calling `all`). You could iterate through `blog_listing` objects and in nested loop iterate over related `blogdetailpage`. The right solution depends on what you want to achieve in result. – Charnel Jan 25 '20 at 11:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206625/discussion-between-psionman-and-charnel). – Psionman Jan 25 '20 at 11:11