开发者

Dynamically Display field values in Django template (object.x)

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 typica开发者_高级运维l 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


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/


As mentioned above, you can do this with a custom template filter.

For example:

@register.filter(name='get_attr')
def get_attr(obj, field_name):
    if isinstance(obj, dict):
        return obj.get(field_name)
    return getattr(obj, field_name)

Then, using it in your template:

{{ obj|get_attr:'business' }}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜