Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class PostListSerializer(serializers.Serializer):
slug = serializers.SerializerMethodField()
title = serializers.SerializerMethodField()
excerpt = serializers.SerializerMethodField()
# get translation to generate slug, title and excerpt
translation = None
def get_translation(self, post):
self.translation = post.translations.order_by('created_at').only('title', 'content')[0]
def get_title(self, obj):
if(self.translation is None):
self.get_translation(obj)
return self.translation.title
def get_slug(self, obj):
if(self.translation is None):
self.get_translation(obj)
return slugify(self.translation.title, obj.id)
def get_excerpt(self, obj):
if(self.translation is None):
self.get_translation(obj)
return get_excerpt(self.translation.content)[:100]
class Meta:
fields = (
'slug',
'title',
'excerpt',
'created_at'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment