@@ -152,6 +152,30 @@ def __str__(self):
152
152
register_snippet (BlogCategory )
153
153
154
154
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
+
155
179
class BlogListingPage (RoutablePageMixin , Page ):
156
180
"""Listing page lists all the Blog Detail Pages."""
157
181
@@ -171,6 +195,15 @@ class BlogListingPage(RoutablePageMixin, Page):
171
195
FieldPanel ("custom_title" ),
172
196
]
173
197
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
+
174
207
def get_context (self , request , * args , ** kwargs ):
175
208
"""Adding custom stuff to our context."""
176
209
context = super ().get_context (request , * args , ** kwargs )
0 commit comments