Skip to content

Commit d7a14bd

Browse files
committed
Serializing Child Pages
1 parent 72fdc61 commit d7a14bd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

blog/models.py

+33
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ def __str__(self):
152152
register_snippet(BlogCategory)
153153

154154

155+
class BlogChildPagesSerializer(Field):
156+
def to_representation(self, child_pages):
157+
# logic in here
158+
return_posts = []
159+
for child in child_pages:
160+
post_dict = {
161+
'id': child.id,
162+
'title': child.title,
163+
'slug': child.slug,
164+
'url': child.url,
165+
}
166+
return_posts.append(post_dict)
167+
return return_posts
168+
# Pythonic comprehensions
169+
# return [
170+
# {
171+
# 'id': child.id,
172+
# 'title': child.title,
173+
# 'slug': child.slug,
174+
# 'url': child.url,
175+
# } for child in child_pages
176+
# ]
177+
178+
155179
class BlogListingPage(RoutablePageMixin, Page):
156180
"""Listing page lists all the Blog Detail Pages."""
157181

@@ -171,6 +195,15 @@ class BlogListingPage(RoutablePageMixin, Page):
171195
FieldPanel("custom_title"),
172196
]
173197

198+
api_fields = [
199+
APIField("posts", serializer=BlogChildPagesSerializer(source='get_child_pages')),
200+
]
201+
202+
@property
203+
def get_child_pages(self):
204+
return self.get_children().public().live()
205+
# return self.get_children().public().live().values("id", "title", "slug")
206+
174207
def get_context(self, request, *args, **kwargs):
175208
"""Adding custom stuff to our context."""
176209
context = super().get_context(request, *args, **kwargs)

0 commit comments

Comments
 (0)