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
15
from rest_framework import serializers
from posts.models import Post
class PostSerializer(serializers.HyperlinkedModelSerializer):
# username = serializers.SerializerMethodField('get_username_from_author')
username = serializers.CharField(source='author__username')
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