format
Shortcut for supplying a
new-style format string
(the "{}".format()
variety) to a column value.
This helper must be called inline with the column definition, in order to provide the format string. It will returned a stand-in callback to defer the formating until the callback is needed.
This is a useful helper for adding things like locale digit separators to large numbers, etc.
As a special case, if the column value is the wrong data type for the format string, you can
provide a cast
keyword argument to the helper to specify a type object that the
value will be converted to. See the examples below.
datatable_options = { 'columns': [ # Standard use; must call the helper once to give it the format string ("Comments", 'n_comments', helpers.format("{:,}"), # If type cast is required first, you can provide that type ("2-places float", 'string_data', helpers.format("{:.2f}", cast=float), ], }
Using this helper in custom callbacks provides no tangible benefit over formatting the string yourself, but can still be done:
def get_column_Comments_data(self, instance, *args, **kwargs): # Generate the real callback helper, then call it callback = helpers.format('{:,}') return callback(kwargs['default_value']) # This is equivalent real Python code, and better for readability return kwargs['default_value'].format('{:,}')