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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:04:12+00:00 2026-05-12T05:04:12+00:00

Thank to this post I’m able to easily do count and group by queries

  • 0

Thank to this post I’m able to easily do count and group by queries in a Django view:

Django equivalent for count and group by

What I’m doing in my app is displaying a list of coin types and face values available in my database for a country, so coins from the UK might have a face value of “1 farthing” or “6 pence”. The face_value is the 6, the currency_type is the “pence”, stored in a related table.

I have the following code in my view that gets me 90% of the way there:

def coins_by_country(request, country_name):
    country = Country.objects.get(name=country_name)
    coin_values = Collectible.objects.filter(country=country.id, type=1).extra(select={'count': 'count(1)'},
                               order_by=['-count']).values('count', 'face_value', 'currency_type')
    coin_values.query.group_by = ['currency_type_id', 'face_value']
    return render_to_response('icollectit/coins_by_country.html', {'coin_values': coin_values, 'country': country } )

The currency_type_id comes across as the number stored in the foreign key field (i.e. 4). What I want to do is retrieve the actual object that it references as part of the query (the Currency model, so I can get the Currency.name field in my template).

What’s the best way to do that?

  • 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-12T05:04:13+00:00Added an answer on May 12, 2026 at 5:04 am

    select_related() got me pretty close, but it wanted me to add every field that I’ve selected to the group_by clause.

    So I tried appending values() after the select_related(). No go. Then I tried various permutations of each in different positions of the query. Close, but not quite.

    I ended up “wimping out” and just using raw SQL, since I already knew how to write the SQL query.

    def coins_by_country(request, country_name):
        country = get_object_or_404(Country, name=country_name)
        cursor = connection.cursor()
        cursor.execute('SELECT count(*), face_value, collection_currency.name FROM collection_collectible, collection_currency WHERE collection_collectible.currency_type_id = collection_currency.id AND country_id=%s AND type=1 group by face_value, collection_currency.name', [country.id] )
        coin_values = cursor.fetchall()
        return render_to_response('icollectit/coins_by_country.html', {'coin_values': coin_values, 'country': country } )
    

    If there’s a way to phrase that exact query in the Django queryset language I’d be curious to know. I imagine that an SQL join with a count and grouping by two columns isn’t super-rare, so I’d be surprised if there wasn’t a clean way.

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

Sidebar

Related Questions

Thank you all for helping. Below this post I put the corrected version's of
First, a thank you in advance. Second, this is my first post so apologies
This post on SO answers most of the questions I have (much thanks to
I dont think this is possible in c# but ill post it anyway. Given
This is related to this post . I think I am having problem with
I was reading this post and had a question that I didn't think it
Backbone.js sents an POST on updated objects instead of an PUT. I think this
This question is related to this SO post Rather than using a recursive CTE
thank you for looking i got this example from my book but i cant
thank you everyone for helping me out with this. i am new to ruby

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.