Django iterating - calculating a sum
I'm trying to iterate through some values, and calculate a rank. I have a calculate_rank function where I calculate a sum of values. The problem is at the second function. I want that a user's rank to be the sum of all the user that in a follow relation with him. I am doing an iteration in the second function here where I try to add the rank of all the users that are in a follow relation with the user sent as a parameter. My problem is that the returned value is zero (0). I am sure I am mistaking in the second function, but I don't see: where?
def calculate_rank(user):
rank = calculate_questions_vote(user) + calculate_votes(user) + calculate_replies(user)
return rank
def calculate_followers_rank(user):
follower = Relations.objects.filter(follow = user)
follower_rank= 0
开发者_JAVA百科 for a in follower:
follower_rank += calculate_rank(follower)
return follower_rank
You're passing follower
- ie the full list of followers - into the calculate_rank
function. I think you either want a
(the current follower in the iteration) or user
(the original user being followed) here.
These things would be easier to spot if you gave your variables more accurate names. If you'd called the list of followers followers
, in the plural, then you'd see it wouldn't make sense to pass it into calculate_rank.
精彩评论