Reputation: 5119
I'm retrieving a list of pages via a custom API endpoint called sitemap
. The goal of this endpoint is to return just the URL and last_updated flag so that I can generate a sitemap.xml file for my website. I can't apply a custom serializer to the model as I don't want to affect the normal pages API endpoint we're using.
Is it possible to apply a serializer to an API queryset rather than serializing it at the model level?
I could probably do this with a list comprehension, but a custom serializer feels like a better solution.
Upvotes: 0
Views: 347
Reputation: 5119
So it turns out that it's much easier than I thought. I created a custom serializer:
class SitemapSerializer(serializers.Serializer):
page_name = serializers.CharField()
url_slug = serializers.CharField()
Then applied that serializer to my APIEndpoint class:
class SitemapPagesAPIEndpoint(PagesAPIEndpoint):
base_serializer_class = SitemapSerializer
I think the thing that threw me off was that the property wasn't called serializer_class
.
Upvotes: 0