Question

I have constructed a website that utilises django-tables2 for presenting tabular data. I have run into a problem which I haven't been able to solve.

In some tables I construct a column verbose_name using a value I pull from the db:

setting = Setting.objects.get(site=Site.objects.get_current())
vol_unit = setting.volume_unit
base_vol_unit = Unit.objects.get(id=settings.BASE_VOLUME_UNIT)
total_vol = tables.Column(
    verbose_name="Total Volume (" + vol_unit.symbol + ")")

This works well, up until a user changes their default volume unit setting. It isn't reflected in the table until the next server restart.

I imagine there will be something I can insert into the table's __init__ method, but I haven't been able to nut it out.

Any ideas?

Many thanks

Nathan

Was it helpful?

Solution

A little late answer but better late than never :)

This is a little tricky, however its not difficult when you get the idea - you actually don't need to change or inherit anything from django-tables2, python provides you with all the tools you need :)

So, the problem is that if you pass a string as the verbose name it is evaluated only once to find out its value and then it's just a static string. What you really need to do is to pass something that would act as a string but its value would be re-evaluated each time.

Well, this is just a normal class that has a __unicode__ method, something for instance like this:

import datetime
class Foo:
    def __unicode__(self):
        return unicode(datetime.datetime.now())

and then you can do something like that in your table:

class MyTable(tables.Table):
    koko = tables.Column(verbose_name=unicode(datetime.datetime.now()) )
    koko2 = tables.Column(verbose_name=Foo())

The header of the koko column will always have the same value but the header of koko2 will change each time you refresh the page!

Python FTW !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top