I'm probably doing something wrong in my template now, but that
version is only returning one event per region, and the order is
backwards. I tried .order_by('-start_date'), but it didn't appear to
make any difference.


On Jul 26, 3:12 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> > few minor questions:
> > missing )
> > [(region, future_events.filter(club__region=region) for region in regions]
>
> > guessing the missing ) goes here:
> > [(region, future_events.filter(club__region=region)) for region in regions]
>
> Yup...must have been my poor transcription from brain to email.
> Sorry for the confusion.  It's building a list of tuples where
> the first item is the region and the second is the list of events
> in that region.
>
> > What happens if there are no events in one of the regions?
>
> > Does that cause a hit to the DB for each region?
>
> In its current state, yes, it does cause a DB hit for each
> region, data or no data.  Yes, Baxter, filter() is lazy, but it's
> instantiated in the view when called upon, so it is actually
> evaluated even when blank.
>
> To avoid this, an alternative might be something like
>
>    future_events = Event.object.filter(
>      start_date__gte=now).select_related(
>      ).order_by('start_date')
>
>    events = {} # or SortedDict if order matters
>    for event in future_events:  # one DB hit here
>      region = event.club.region
>      ev_list = events.get(region, [])
>      ev_list.append(event)
>      events[region] = ev_list
>    return render_to_response(..., {
>      'events': events, # this might need to
>         # be events.iteritems() or something
>         # like that
>      ...})
>
> This version should only hit the DB once for all the event data
> and also sort the data before it ever gets to you, so you
> (Baxter) can avoid the dictsort in your template.
>
> -tim


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to