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 7581869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:17:58+00:00 2026-05-30T18:17:58+00:00

In Django, I have an app with models like this: class Artist(models.Model): name =

  • 0

In Django, I have an app with models like this:

class Artist(models.Model):
    name = models.CharField()

class Song(models.Model):
    artist = models.ForeignKey(Artist)
    title = models.CharField()

class SongPlay(models.Model):
    song = models.ForeignKey(Song)
    time = models.DateTimeField()

I want to be able to do a chart of the most played songs and the most played artists. How can I use Django’s ORM to do a GROUP BY and a COUNT like this?

At the moment, I’m doing something like this:

SongPlay.objects.values('song__id', 'song__artist__name', 'song__title')
    .annotate(Count('song')).order_by('-song__count')[:10]

…to get the top 10 songs, and this:

SongPlay.objects.values('song__artist__id', 'song__artist__name')
    .annotate(Count('song__artist')).order_by('-song__artist__count')[:10]

…to get the top 10 artists.

Is there an easier way to achieve this? I don’t really like specifying the values, I’d rather get tuples along the lines of (song_object, count) and (artist_object, count).

Is there something obvious I’ve missed? Thanks!

  • 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-05-30T18:18:00+00:00Added an answer on May 30, 2026 at 6:18 pm

    You should start building your queries from the objects that you want to get in the end.

    Artist.objects.annotate(played=Count('song__songplay')).order_by('-played')[:10]
    Song.objects.annotate(played=Count('songplay')).order_by('-played')[:10]
    

    Then queries will be much clearer.

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

Sidebar

Related Questions

In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have a model like this: from django.db import models class House(models.Model): address =
Django newbie here. I have two simple models like this: class Fluff(models.Model): # ....
Let's say I've defined this model: class Identifier(models.Model): user = models.ForeignKey(User) key = models.CharField(max_length=64)
Am using django forms on my app. I have a model Client like this:
I have a django project with 2 apps like this: ## tags app, models.py
I have these 3 models in models.py class Customer(models.Model): name = models.CharField(max_length=50) .... class
I've got a basic Item model in my Django app: class Item(models.Model): name =
I have an existing app with the following model class Contact(models.Model): lastname = models.CharField(max_length=200)

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.