(tables) How to add a new column after the class is already built

6 views
Skip to first unread message

brfox

unread,
Jan 8, 2010, 1:33:21 PM1/8/10
to Django Apps
First off, thank you for making this nice app. I think it is really
well-written and robust.

I just wanted to share how I figured out how to add a column after the
class has already been built. I am doing this because I wanted my
table to be a little more dynamic - I want the users to be able to add
columns, not just changing their visibility, but pulling in a couple
columns from many (100+ choices). I didn't want to hard code 100+
columns to the table.Table class, but instead just add them in on the
fly.

It is really quite simple, thanks to the nicely written django-tables:

# allrows will have a list of rows based on the django Model
allrows = DjangoModel.objects.filter(pk_lt=10).values()

# add a new column
for row in allrows:
row["new_data"] = 1

# make a new django-table object
table_object = MyTable(allrows)

# add the new data as a column
table_object.base_columns["new_data"]

The nice thing about table_object is that it has all the data in it,
regardless of whether or not there is a column already specified for
it. So, adding a new column via base_columns just allows that data to
be accessed via the template functions, whereas before it is
inaccessible.

Reply all
Reply to author
Forward
0 new messages