from rest_framework import serializers from posts models import Post c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from rest_framework import serializers
from posts.models import Post
class PostSerializer(serializers.HyperlinkedModelSerializer):
username = serializers.SerializerMethodField('get_username_from_author')
class Meta:
model = Post
fields = ['title', 'body', 'date_published', 'date_updated', 'post_like', 'post_unlike', 'username']
def get_username_from_author(self, post):
username = post.author.username
return username