views:

416

answers:

1

Hi Guys,

I am currently working on an app that uses custom annotate querysets. Currently i have 2 urls setup, but i would need one for each field that the users would like to summarize data for. This could be configured manually, but it would violate DRY! I would basically have +-8 urls that basically do the same thing.

So here is what i did,

  1. I have a created custom model manager
  2. I have a view
  3. I have the URLS configured

All of the above works.

So basically the URL config passes to the view the name of the field to annotate by (group by for SQL folks), the view does some additional processing and runs the custom model manager based on the field that was passed to it.

The URL looks like this:

url('^(?P<field>[\w-]+)/(?P<year>\d{4})/(?P<month>\d+)/(?P<day>\d+)/$','by_subtype', name='chart_link'),

The field is the column in db the that is used when the queryset is actually run. It is passed from the view, to my custom manager. Below is an example of the code from the manager:

return self.filter(start_date_time__year=year).filter(start_date_time__month=month).filter(start_date_time__day=day).values(field).annotate(Count(field))

In addition, i pass the value of field as context variable. This is used to dynamically build the links. However the problem is actually looping through the query set and displaying the data.

So your typical template code looks like this:

{% for object in object_list %}
{{ object.sub_type }} : {{ object.sub_type__count|intcomma }}
{% endfor %}

Basically you have to hard code the field to diplay (i.e object.x), is there anyway to dynamically assign this? i.e if field = business then in the template it should automatically process:

{{ object.business }}

Can this be done? Or would i need to create several URLS? Or is there a better way to achieve the same result, a single view and url handling queries dynamically.

You can find the code over at github, the template part is now working using this snippet: http://www.djangosnippets.org/snippets/1412/ So if you come across this later and want to do something similar have a look at the code snippet at github. : http://gist.github.com/233262

A: 

It sounds like you want to do something along the lines of:

# in the views.py:
field = 'business'


{# in the template: #}
{{ object.field }}

and have the value of object.business appear in the output. This isn't possible with the Django template language out of the box.

There are snippets that define template filters you can use to accomplish this though: http://www.djangosnippets.org/snippets/1412/

Ned Batchelder
Hi Ned, yes this is exactly it. I tried creating a custom templatetag, but i think the issue has to do with how the templatetag / template etc. is parsed, the snippet looks like it could work. Will give it a shot.
izzy
thanks this worked....
izzy