Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7836593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:13:18+00:00 2026-06-02T14:13:18+00:00

I am building an application that tracks karma that users give to each other.

  • 0

I am building an application that tracks karma that users give to each other. I want to be able to build a leaderboard over a rolling 7-day period and a list of people who have given you the most karma over a rolling 7-day period. I have come up the following model:

class KarmaAction(models.Model):
    user = models.ForeignKey(User)
    sender = models.ForeignKey(User)
    karma = models.IntegerField()
    created_at = models.IntegerField()

First off, will the above model scale well as the number of users grows? I am thinking of deleting any rows that are over 7 days old. Secondly, I want to know how I can create a list of senders with the corresponding sum of the karma they have given the last few days. Maybe it would look something like this:

supporters = {'User 1': 14, 'User 2': 12, 'User 3': 7, 'User 4': 2}

Is there any way to do this with the Django ORM?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:13:21+00:00Added an answer on June 2, 2026 at 2:13 pm

    The model seems reasonable to me; I don’t really see another way to do it. You need all of the information in there to be able to fulfill your requirements, anyway. (Though created_at should probably be a DateTimeField?) If it ends up too slow, you’ll probably just have to add aggressive caching.

    You can do something like (untested, but I think it should work)

    TIME_RANGE = datetime.timedelta(days=7)
    now = datetime.datetime.now()
    
    KarmaAction.objects.filter(created_at__gte=now - TIME_RANGE) \
                       .filter(user=recepient_user) \
                       .values('sender') \
                       .annotate(total_sent=Sum('karma')) \
                       .order_by('total_sent')
    

    which will use the values to group by unique senders and then sum their karma values.

    I added the order_by to make sure that if there’s a default ordering, it gets cleared and doesn’t interfere with the grouping. It also makes it easy to just get the top n senders of karma, though, by just adding e.g. [:5] to the end — if you make it -total_sent, of course :).

    I don’t think the filter causes this problem, but I’m not positive without testing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an application that is used by several different customers. Each customer has
Building a rails B2B application that will have various users. I'm pretty clear on
I am building an application that perform checks on users on specific days. I
I am building an application that users can add, update data. Is there any
I'm building an application that lets my users create several markers on a map.
I am building application that required some data from iPhone's Call log(read only). The
I am building an application that has virtual windows. The output is TImage object.
I'm building an application that is targeting Windows, Mac and Linux soon. I was
We're currently building an application that executes a number of external tools. We often
I am considering building an application that is a blend of a dynamic language

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.