tags:

views:

37

answers:

1

Hi,

I have a simple view in Django:

@render_to('episode_list.html')
def list_episodes(request, season):
    query = Episode.objects.filter(season=season)
    seasons = query[0].series.total_seasons    
    return  {'episodes': query,
             'season': season,
             'max_seasons':range(1,seasons + 1)}

I'm trying to build the navigation on my template dynamically and need 'max_seasons' to do it... Is there some better way to get this information, since it seems like this would make an extra database query.

The '.series' is referring to a foreign key.

A: 

Tweaking it to be query = Episode.objects.select_related('series').filter(season=season) would follow the fk in the first DB hit, meaning you wouldn't need another, but it will pull/pre-fetch more Series results than the single one you (seem to) need if the queryset query contains more than one result (perhaps better to call it episodes for clarity?).

(But, even if it does pull more than you need, it is likely to still be faster)

Official docs are here

stevejalim
Very cool, thanks a lot.
tkorg