def get(self, request: HttpRequest, board_id: int, article_id: int): comments = Comment.objects.all().filter(article_id=article_id) serializer = CommentListSerializer(comments, many=True) output = dict() output['data'] = serializer.data output['msg'] = 'SUCCESS' return Response(output, status=status.HTTP_200_OK)
def list_comments(product_slug): page_size = request.args.get('page_size', 5) page = request.args.get('page', 1) product_id = Product.query.filter_by( slug=product_slug).with_entities('id').first()[0] comments = Comment.query.filter_by(product_id=product_id).order_by( desc(Comment.created_at)).paginate(page=page, per_page=page_size) return jsonify( CommentListSerializer(comments, include_user=True).get_data()), 200
class BlogDetailsSerializer(serializers.ModelSerializer): comments = CommentListSerializer(many=True, read_only=True) category = CategoryListSerializer( many=True, read_only=True) class Meta: model = Blog fields = ['id', 'title', 'author', 'content', 'status', 'thumbnile', 'updated_on', 'slug', 'category', 'comments', 'created_on']
def get_comments(self, obj): request = self.context.get('request') limit = 10 if request: limit_query = request.GET.get('comments_limit') try: limit = int(limit_query) except: pass c_qs = Comment.objects.filter_by_instance(obj) comments = CommentListSerializer(c_qs[:limit], many=True, context={ 'request': request }).data return comments
def __init__(self, product): self.data = { 'success': True, 'id': product.id, 'name': product.name, 'description': product.description, 'price': product.price, 'stock': product.stock, 'slug': product.slug, 'comments': CommentListSerializer(product.comments.all(), include_user=True).data, 'tags': [tag.name for tag in product.tags], 'categories': [category.name for category in product.categories], 'image_urls': [image.file_path.replace('\\', '/') for image in product.images] }
def get_dto(article, include_details=False): response = { 'id': article.id, 'title': article.title, 'description': article.description, 'slug': article.slug, 'user': { 'id': article.user_id, 'username': article.user.username }, 'tags': [{'id': tag.id, 'name': tag.name, 'slug': tag.slug} for tag in article.tags], 'categories': [{ 'id': category.id, 'name': category.name, 'slug': category.slug} for category in article.categories] } if include_details: response['comments'] = CommentListSerializer(article.comments, include_user=True).data else: response['comments_count'] = len(article.comments) return response
def comments(self, request, pk): comments = self.get_object().comments.filter(parent=None) serializer = CommentListSerializer(comments, many=True) return Response(serializer.data)
def get_comments(self, obj): c_qs = Comment.objects.filter_by_instance(obj) comments = CommentListSerializer(c_qs, many=True).data return comments
def get_comments(self, obj): object_id = obj.id c_qs = Comment.objects.filter(post=object_id) comments = CommentListSerializer(c_qs, many=True).data return comments
def get_comments(obj): content_type = obj.get_content_type object_id = obj.id c_qs = Comment.objects.filter_by_instance(obj) comments = CommentListSerializer(c_qs, many=True).data return comments
def get_comments(self, obj): serializer = CommentListSerializer(obj.comments(), many=True) return serializer.data