Live demo

blog headline Age


Description

Columns that have values derived at runtime by callbacks located on the view should specify their field as None, and then provide a callback name or function reference.

Callbacks should take the instance represented by the row, and *args and **kwargs to maximize flexibility with future data arguments sent by the callback dispatcher.

Callbacks can be used for concrete fields too.

The Age column could actually still specify its model field as pub_date, but it was omitted specifically to demonstrate the bridging of the gap by the callback. In this situation, the column is not searchable or sortable at the database level.



Implementation in code

        
    from django.template.defaultfilters import timesince
    class VirtualColumnDefinitionsDatatableView(DatatableView):
        model = Entry
        datatable_options = {
            'columns': [
                'blog',
                'headline',
                ("Age", None, 'get_entry_age'),
            ],
        }

        def get_entry_age(self, instance, *args, **kwargs):
            return timesince(instance.pub_date)