(django-tables) Accessing data from the template

28 views
Skip to first unread message

AJ

unread,
Feb 12, 2009, 9:40:43 PM2/12/09
to Django Apps
Here is my table class:
--------------------------------------------
import django_tables as tables


class SomeTable(tables.ModelTable):
name = tables.Column(data="something__data__name")
rating = tables.Column(data="rating")
date = tables.Column(data="date_added")

class Meta:
pass
--------------------------------------------
Then here is a snippet from my view
--------------------------------------------
def some_view(request, user_id):
ratings = Rating.objects.filter(user__id=user_id)
table = RatingsTable(ratings, order_by=(request.GET.get(
"sort", "rating")))
--------------------------------------------
In my template i've assigned the table var in the view to a table var
in the template

Accessing the columns headers works fine
{% for column in table.columns %}
...
{% endfor %}

Looking at the README it looks like you can access the data directly
from the row:
for country in countries.rows:
print country.city.id
print country.city.geo
print country.city.founder.name

However, I have to prefix it with data so the above example would give
me an attribute error
here is what I would have to do
for country in countries.rows:
print country.data.city.id
print country.data.city.geo
print country.data.city.founder.name

Are you supposed to be able to access the data directly when fetching
each row, or do you need to use row.data.field? I wasn't sure if I
set mine up incorrectly since I didn't have all of the fields as
attributes, just data, table, and as_html.

Thank,s
Aaron

Michael Elsdörfer

unread,
Feb 12, 2009, 11:20:54 PM2/12/09
to Django Apps
You're doing it right. The readme file is somewhat confusing in that
point (I fixed it in r44 - thanks). What is meant is that you can
access those fields once you have defined a column for the
relationship.

Michael

AJ

unread,
Feb 13, 2009, 7:02:00 AM2/13/09
to Django Apps
I understand, thanks for the information, this is a really useful app.
Reply all
Reply to author
Forward
0 new messages