views:

79

answers:

3

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

http://stackoverflow.com/questions/327807/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?

A: 

Have you tried select_related() http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4

I use it a lot it seems to work well then you can go coin_values.currency.name.

Also I dont think you need to do country=country.id in your filter, just country=country but I am not sure what difference that makes other than less typing.

Justin Hamade
A: 

You can't do it with values(). But there's no need to use that - you can just get the actual Collectible objects, and each one will have a currency_type attribute that will be the relevant linked object.

And as justinhamade suggests, using select_related() will help to cut down the number of database queries.

Putting it together, you get:

coin_values = Collectible.objects.filter(country=country.id, 
                    type=1).extra(
                    select={'count': 'count(1)'}, 
                    order_by=['-count']
                ).select_related()
Daniel Roseman
A: 

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.

Jason Champion