I have a home app with HomePage
model which gets object from BlogPage
model in blog app. I use this code below the obtain the object from BlogPage
to HomePage
.
blog app
class HomePage(Page):
primary_header = models.CharField(max_length=100, blank=True,)
secondary_header = models.CharField(max_length=100, blank=True,)
hero_banner_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=False,
on_delete=models.SET_NULL,
related_name='+')
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
# Get object from Blog Page
blogpages = self.get_children().get(title='Blog Page').get_children().live().public().order_by('-first_published_at')
context['blogpages'] = blogpages
return context
I used get()
to obtain the object by title, but I feel that it may not be the best way to do it. If I were to rename the BlogPage
title field to for example, "The Blog Page", it will break the code since 'Blog Page' is no more used. Is there a better way to queryset from BlogPage
to HomePage
model?
home app
class BlogPage(Page):
subpage_types = ['PostPage']
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
category_slug = request.GET.get('category', None)
context['categories'] = BlogCategory.objects.all()
return context