I haven't seen any question like this on the site. So hopefully this question will be useful for others in the future.
I have a PostPage, EditorialPage, and DocumentPage. Within the models of the three, I added a 'featured' Boolean, so that if toggled in the settings, either Post is featured onto the HomePage.
However, in the HomePage/Models, I only was able to get it to work with PostPage. I'm not too keen with query/querysets, but I feel it has something to do with that. I've read the Docs multiple times, but I do not understand how to make this work.
Blog/Models.py
class PostPage(Page):
header_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
featured = models.BooleanField(default=False)
highlight = models.BooleanField(default=False)
description = models.CharField(max_length=255, blank=True,)
body = StreamField(BodyBlock(), blank=True)
tags = ClusterTaggableManager(through="blog.PostPageTag", blank=True)
post_date = models.DateTimeField(
verbose_name="Post date", default=datetime.datetime.today
)
content_panels = Page.content_panels + [
ImageChooserPanel("header_image"),
FieldPanel("description", classname="full"),
InlinePanel("blog_authors", label="author", min_num=1, max_num=4),
MultiFieldPanel([
InlinePanel("categories", label="category",),
FieldPanel("tags"),
], heading="Meta"),
StreamFieldPanel("body"),
]
settings_panels = Page.settings_panels + [
FieldPanel("post_date"),
MultiFieldPanel([
FieldPanel("featured"),
FieldPanel("highlight"),
], heading="Showcase")
]
Home/Models.py (Method 1)
class HomePage(RoutablePageMixin, Page):
def get_context(self, request):
featured_post = PostPage.objects.live().public().filter(featured=True)
featured_editorial = EditorialPage.objects.live().public().filter(featured=True)
feature_document = DocumentPage.objects.live().public().filter(featured=True)
featured_select = sorted(
chain(featured_post, featured_editorial, feature_document),
key=lambda page: page.first_published_at, reverse=True)[0]
return featured_select
Home/Models.py (Method 2)
class HomePage(RoutablePageMixin, Page):
def get_context(self, request):
context = super().get_context(request)
featured_post = PostPage.objects.live().filter(featured=True)
featured_post = EditorialPage.objects.live().filter(featured=True)
featured_post = DocumentPage.objects.live().filter(featured=True)
context['featured_post'] = featured_post.live().public()
return context
def get_posts(self):
return PostPage.objects.descendant_of(self).live().order_by("-post_date")
Thank you in advance to anyone that reaches out to this post. I just started using Python in November. So I'm still fresh, I think.