1

I have to create an algorithm that gets the most popular users of my site. I don't really know how to start. I have to take in consideration : - number of follower - number of comment / topic posted - number of positive vote received by the user - number of group membership

I tried something like this :

 UserProfile.objects.annotate(num_topic=Count('topic')).order_by('-num_topic')

This give me the list of userProfile ordered by the most topic but I don't know how to combine all of them.

comment / topic / group / likeComment are table that have a foreign key to the user. Follower / Following is a ManyToManyField through a table named relationship.

Any help would be much appreciated

So far that's where I am :

topUsers = UserProfile.objects.annotate(num_topic=Count('topic',    distinct=True)).annotate(num_comment=Count('comment', distinct=True))

for user in topUsers:
    user.positive_votes = LikeComment.objects.filter(Q(type = 1), Q(comment__user=user) |  Q(topic__user=user) ).count()
    user.negaive_votes = LikeComment.objects.filter(Q(type = 0), Q(comment__user=user) |  Q(topic__user=user) ).count()

    user.num_group = GroupUser.objects.filter(user = user, status=1).count()
    user.num_followers = user.related_to.filter(from_people__status=1, from_people__to_person=user).count()
    user.num_followings = user.relationships.filter(to_people__status=1, to_people__from_person=user).count()

But when I do

topUsers.order_by('-num_followers','-num_topic','-num_comment','-positive_vote','-num_group','-num_followings')

it doesn't order them.

I tried to use extra fields but I got lost in my sql ...

Thanks

4

0 回答 0