I am using Streamfields in Wagtail and got the following problem:
I would like to print out the image path instead of the id and followed the instructions here: Custom representation of Streamfield in rest API
But it seems that get_api_representation() is never called in my case.
urls.py
router.register(r'page', PageView, basename='page')
views.py
class PageView(viewsets.mixins.RetrieveModelMixin, GenericViewSet):
queryset = Page
serializer_class = PageSerializer
serializers.py
class PageSerializer(serializers.ModelSerializer):
class Meta:
model = Page
fields = ('title', 'body')
models.py
class APIImageChooserBlock(ImageChooserBlock):
def get_api_representation(self, value, context=None):
if value:
return {
'id': value.id,
'title': value.title,
'image': value.get_rendition('width-1000').attrs_dict
}
class ImageTextBlock(blocks.StructBlock):
image = APIImageChooserBlock()
text = blocks.CharBlock()
class Page(wagtail_models.Page):
body = StreamField(
[
('MyBlock', ImageTextBlock()),
('Newsletter', blocks.StructBlock([
('text', blocks.RichTextBlock()),
])),
]
)
content_panels = wagtail_models.Page.content_panels + [
StreamFieldPanel('body', classname="full", heading=_('Content'))
]
Can somebody identify the problem here?