Live demo

ID headline body text blog Publication Date


Description

If a column definition hasn't supplied an explicit callback value processor, there is a default method that will be looked up on the view instance in the format of get_column_FOO_data().

If the field has defined a "pretty name" in the tuple format, the pretty name will be used for the basis of looking up this default callback. This is to avoid the complexities of mangling an automatic name that makes sense for compound and virtual columns.

"Pretty names" put through the mangling process essentially normalize non-letter non-number characters to underscores, and multiple adjacent underscores are collapsed to a single underscore. It's like a slugify process, but using "_" and without lowercasing.

If the name mangling is ever unintuitive or cumbersome to remember or guess, you can either supply your own callback names as the third item in a column's 3-tuple definition, or else define a callback using the 0-based index instead of the field name, such as get_column_0_data().

The default hook is only executed if there is no callback already supplied in the column definition.



Implementation in code

        
    class DefaultCallbackNamesDatatableView(DatatableView):
        model = Entry
        datatable_options = {
            'columns': [
                'id',
                'headline',
                'body_text',
                'blog',
                ("Publication Date", 'pub_date'),
            ],
        }

        def get_column_body_text_data(self, instance, *args, **kwargs):
            return instance.body_text[:30]

        def get_column_Publication_Date_data(self, instance, *args, **kwargs):
            return instance.pub_date.strftime("%m/%d/%Y")