verdic
class BlogCommentSerializer(serializers.ModelSerializer):
author_name = serializers.SerializerMethodField()
class Meta:
model = BlogComment
fields = ['id', 'blog_post', 'author', 'author_name', 'content', 'created_at', 'updated_at']
read_only_fields = ['author', 'blog_post']
def get_author_name(self, obj):
return obj.author.name
def create(self, validated_data):
validated_data['author'] = self.context['request'].user
return super().create(validated_data)