shymonk/django-datatable

[SUGGESTION] Implements a Util to run functions

rodolphopivetta opened this issue · 2 comments

This is inspired in utils.Accessor class

Implements a method that call functions inside fields in table, like:

from table.utils import B

def my_function(my_dict):
    return [d['name'] for d in my_dict]

preferences = Column(field=B(my_function(A('preferences.values'))), header=_(u'Preferences'))

The preferences field in model is a ManyToManyField

This will produce something like:

>>> preferences.values
[{u'id': 1, 'name': u'One thing'}, {u'id': 2, 'name': u'Another thing'}]

>>> my_function(preferences.values)
[u'One thing', u'Another thing']

This is only an example, but with this fixture, it's possible to run any method that return a string to be used in field of Collumn. (Or another usage).

Hi, rodolphopivetta, thanks for your suggestion, but building a B utility to influence render process of column maybe not a good design.
Accessor(A) is designed to make it possible to access row object field as column cell attribute, such as headers or title.
So I highly recommand to define your own Column subclass and override render function if you want to customize how column rendering.
I have considered to build a interface render_{column_name} for Table class to do this job before just like this:

class FooTable(Table):
    preferences = Column(field='preferences.values', header=_(u'Preferences'))

    def render_preferences(self, object, field):
        return your_function(Accessor(field).resolve(obj))

But i'm too lazy to implement. :)

you're right, override the render method is much better.

I really liked your project, this is much useful. Thanks.