def get_comment(self, obj): comments = {} for index, comment in enumerate(obj.get_comments): comments[str(index)] = CommentSerializer(comment).data return comments
def comments(self, request, *args, **kwargs): article = self.get_object() serializer = CommentSerializer(article.comments.all(), many=True, context=self.get_serializer_context()) return Response(serializer.data)
def create(self, request, *args, **kwargs): serializer = CommentSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def getComments(versus): response = [] for comment in Comment.objects.filter(versus=versus).order_by('-date'): response.append(CommentSerializer(comment).data) return (response)
def get_highlighted_comments(self,obj): request = self.context['request'] comments = obj.comments.order_by('-id')[0:2] return CommentSerializer(comments, context={'request': request}, many=True).data
def get(self, request, *args, **kwargs): comments = CommentSerializer(self.get_object().comments, many=True) comments_json = comments.data return Response(comments_json)
def get_comments_data(self, obj): return CommentSerializer(obj.comments, many=True, read_only=True).data
def get_comments_view(request, article_pk): object_to_return = get_object_or_404(Comment, article_id=article_pk).values() return Response(CommentSerializer(object_to_return).data)
class PostSerializer(serializers.ModelSerializer): comments = CommentSerializer(many=True, read_only=True) class Meta: model = Post fields = ('title', 'content', 'comments')
class ArticleCommentSerializer(serializers.ModelSerializer): comments = CommentSerializer(many=True) class Meta: model = Article fields = ('comments', )
def get_related_comment(self,obj): comments = Comment.objects.all().filter(documentation=obj).order_by('-id') serializer = CommentSerializer(comments,many=True,context={'request': self.context['request']}) return serializer.data
def retrieve(self, request, *args, **kwargs): post = self.get_object() serializer = CommentSerializer(post.comments, many=True) return Response(serializer.data)
def get_comment(self, obj): return CommentSerializer(obj.comment).data
def get(self, request, title_pk, format=None): comment = self.get_object(title_pk) serializer = CommentSerializer(comment, many=True) return Response(serializer.data)
def get(self,request): comment_objects = Comment.objects.all() print(list(comment_objects)) # to convert queryset to list of objects, queryset is lazy, it has to be iterred thorugh so as to get the objects which can serialized serialized_comment_objects = CommentSerializer(comment_objects, many=True) return Response(serialized_comment_objects.data)
def get(self,request,comment_id): comment_object = get_object_or_404(Comment,pk=comment_id) serialized_comment_object = CommentSerializer(comment_object) return Response(serialized_comment_object.data)