Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想在django rest框架中注释部分的顶部列表中显示最受欢迎的注释_Python_Django_Django Rest Framework - Fatal编程技术网

Python 我想在django rest框架中注释部分的顶部列表中显示最受欢迎的注释

Python 我想在django rest框架中注释部分的顶部列表中显示最受欢迎的注释,python,django,django-rest-framework,Python,Django,Django Rest Framework,我正在做一个项目,我想在评论区的最热门列表中显示一条最受欢迎的评论,就像Instagram、Facebook一样 这是我的序列化程序PostCommentSerializer:- 类PostCommentSerializer(serializers.ModelSerializer): 以下是我的观点。py添加\u评论\u喜欢的内容:- profilephoto = serializers.SerializerMethodField() likes = serializers.Serialize

我正在做一个项目,我想在评论区的最热门列表中显示一条最受欢迎的评论,就像Instagram、Facebook一样

这是我的序列化程序PostCommentSerializer:-

类PostCommentSerializer(serializers.ModelSerializer):

以下是我的观点。py添加\u评论\u喜欢的内容:-

profilephoto = serializers.SerializerMethodField()

likes = serializers.SerializerMethodField()
likes_count = serializers.SerializerMethodField()

replypostcomment = ReplyPostCommentSerializer(many=True)

class Meta:
    model = PostComment
    fields = ['id', 'commented_by', 'content', 'date',
              'profilephoto', 'replypostcomment', 'likes_count', 'likes']

def get_commented_by(self, obj):
    return obj.commented_by

def get_post(self, obj):
    return obj.post.title

def get_profilephoto(self, obj):
    if profileDB.objects.all().filter(user=obj.commented_by).count() > 0:
        profile = profileDB.objects.all().filter(user=obj.commented_by)
        print(profile)
        return profile[0].profilephoto.url
    else:
        print("---------------------->")

def get_likes_count(self, obj):
    return obj.likes.count()

def get_likes(self, obj):
    p = obj.likes.all()
    lst = []
    for i in range(len(p)):
        lst.append({"id": p[i].id, "username": p[i].username})
    return lst
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def add_comment_likes(request, **kwargs):
    comment_liked = get_object_or_404(PostComment, pk=kwargs.get('i_pk'))
    if comment_liked.likes.filter(pk=request.user.i_pk).exists():
        comment_liked.likes.remove(request.user)
        data = PostCommentSerializer(comment_liked, many=False)
        return Response({'status': status.HTTP_200_OK,'data':data.data})
    else:
        comment_liked.likes.add(request.user)
        data = PostCommentSerializer(comment_liked, many=False)
        return Response({'status': status.HTTP_200_OK,'data':data.data})