Skip to content

Commit 3465f28

Browse files
committed
Serializing a ForeignKey to a Wagtail Page
1 parent fda3b85 commit 3465f28

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

blog/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,4 @@ class VideoBlogPage(BlogDetailPage):
409409
),
410410
FieldPanel("youtube_video_id"),
411411
StreamFieldPanel("content"),
412-
]
412+
]

home/models.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
from wagtail.core.fields import RichTextField, StreamField
1818
from wagtail.images.edit_handlers import ImageChooserPanel
1919
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
20+
21+
from rest_framework.fields import Field
22+
2023
from streams import blocks
2124

2225

@@ -41,6 +44,18 @@ class HomePageCarouselImages(Orderable):
4144
]
4245

4346

47+
class BannerCTASerializer(Field):
48+
def to_representation(self, page):
49+
return {
50+
'id': page.id,
51+
'title': page.title,
52+
'first_published_at': page.first_published_at,
53+
'owner': page.owner.username,
54+
'slug': page.slug,
55+
'url': page.url,
56+
}
57+
58+
4459
class HomePage(RoutablePageMixin, Page):
4560
"""Home page model."""
4661

@@ -79,7 +94,7 @@ class HomePage(RoutablePageMixin, Page):
7994
APIField("banner_title"),
8095
APIField("banner_subtitle"),
8196
APIField("banner_image"),
82-
APIField("banner_cta"),
97+
APIField("banner_cta", serializer=BannerCTASerializer()),
8398
APIField("carousel_images"),
8499
APIField("content"),
85100
]

0 commit comments

Comments
 (0)