Show values in cells which do not come from database fields

Issue #51 invalid
Former user created an issue

Sometimes I want to show computed values in tables. How can I do this with the djanog-datatables-view?

Comments (3)

  1. toabi

    render_column sadly converts to string already so doing super() and then calling the method is result is callable is not possible. I'd insert

    if callable(value):
      value = value()
    

    between the getattr and if value is None. But I just noticed that there's also not yet an easy way to then define how search is handled for those fields. So I think I'll just stick to database fields for now.

    Would you be interested in the feature of having not only database fields as columns, but any callables/properties of models with support for search of those (via custom methods defined in the child of {{BaseDatatableView}}? I'm thinking about doing a PR.

  2. Maciej Wisniowski repo owner

    Sorry, but I don't understand what you mean. You can use render_column like:

    class MyClass(BaseDatatableView):
        def render_column(self, row, column):
            if column == 'mycolumn':
                return '{} and {}'.format(row.field1, row.get_field2_display())  #or compute value in any other way you want
            else:
                return super(MyClass, self).render_column(row, column)
    

    Regarding filtering, just override filter_queryset method in a way you need.

  3. Log in to comment