On Thu, May 19, 2011 at 1:07 PM, Geoff Kuenning <ge...@cs.hmc.edu> wrote:
> Thanks, Tom, that was HUGELY helpful!  I had been thinking about using
> a filter expression, but it's so much easier to have sample code.
>
> I have another question, though (not just for Tom).  The article Tom
> linked to
> (http://groups.google.com/group/django-users/browse_thread/thread/
> 6f4b7d95e4d0c810/abd10cb039f1f68c?show_docid=abd10cb039f1f68c&pli=1)
> says that the Django developers consider this approach poor style
> because the template is doing too much work.  Can somebody point me to
> some hints about a more Django-ish way to accomplish the same result?
> (Basically, what I want is for the view to define the titles and
> ordering of table columns, with the contents taken from a database
> query.  Obviously I could just loop through the whole durned query by
> row and column, rebuilding it into a 2-D array, but that seems
> clumsy...)
>

It was me I linked to, so I feel I should reply :)

I either do it the clumsy way, building a 2D array, or I use a
'custom' filter (it's not that custom, it's a pretty generic tool to
have) to access the values, as described before.

I suppose a more elegant solution would be to write a generator which
produces each new row with the elements in the right order. Something
like this would work:

def table_qs_generator(queryset, columns):
  yield columns
  for obj in queryset:
    # generate row_data from obj and columns
    yield row_data

return render('foo.html', { 'table_data': table_qs_generator(qs, cols) }

This avoids the nastiness of generating the entire table in memory.

Cheers

Tom

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to