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

flo bee
  • 830
  • 2
  • 11
  • 20
  • Maybe django.db.models.Q helps? – Jay Apr 03 '13 at 22:00
  • 2
    This sort of operation should not be done in one query. You should rather compute this daily and update the user popularity field on the user model. Such operation involving many variables will be slow so should be cached. Then you can do a daily cron to recompute the values. – miki725 Apr 03 '13 at 22:09
  • Hi @miki725 compute it daily sound a good idea actually ^^ – flo bee Apr 03 '13 at 23:45

0 Answers0