wagtail / wagtailtrans

A Wagtail add-on for supporting multilingual sites
http://wagtailtrans.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
104 stars 60 forks source link

get_context does not return correct type #177

Closed gannebamm closed 4 years ago

gannebamm commented 4 years ago

Issue summary

For some reason my get_context() does not fetch the correct type of page model. See my code:

class BlogPage(TranslatablePage):
    """single blogpost"""
    # title is part of super()
    blogTeaser = models.CharField(max_length=50, null=True, blank=False)
   [...]
class BlogListingPage(TranslatablePage):
    """list of blogposts"""
    max_count_per_parent = 1

    # add blogposts as children to contexts
    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context['posts'] = self.get_children().filter(
            live=True)
        return context
{% block content %}
    <div class="container ">
        <div class="row row-cols-1 row-cols-md-3">
            {% for post in posts %}
                <div class="col mb-4">
                    <div class="card h-100">
                        {% image post.blogpage.blogImage fill-300x200 as blog_img %}
                        <img src="{{ blog_img.url }}" class="card-img-top" alt="{{ blog_img.alt }}">
                        <div class="card-body">
                            <h5 class="card-title">{{ post.title }}</h5>
                            <p class="card-text">{{ post.blogpage.blogTeaser }}</p>
                            <a href={{ post.url }} class="stretched-link"></a>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>
{% endblock content %}

How to reproduce?

Use the example above and try to fetch post.blogpage.blogTeaser attribute.

Technical details

gannebamm commented 4 years ago

not related to the module. My fault. This works:

class BlogListingPage(TranslatablePage):
    """Liste der Blogposts"""
    max_count_per_parent = 1

    # add blogposts as children to contexts
    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context['posts'] = BlogPage.objects.child_of(self).filter(live=True)

        return context