blog | headline | Publication date |
---|
Model methods can power columns instead of concrete fields. This approach is similar to using a callback to supply a value for a virtual column, but demonstrates that non-field data sources may be used on the model without custom callbacks.
In this situation, the pub_date
is fetched using an example method get_pub_date()
. The column is technically sortable by default, but the operation takes place in code, rather than in the database. Searching is not currently possible because of prohibitive performance penalties for large data sets.
This strategy works nicely for fields that have a choices
list, allowing you to use the get_FOO_display()
method Django puts on the model instance.
class ColumnBackedByMethodDatatableView(DatatableView): model = Entry datatable_options = { 'columns': [ 'blog', 'headline', ("Publication date", 'get_pub_date'), # get_pub_date is an attribute on the model ], }