Open ccsv opened 6 years ago
So you suggest doing some inspection of the thing we get the data from and only set orderable=True
if the column gets its data from a model field?
I didn't think this trough very well, but I suspect that might break the use case of rendering from a list of dicts.
@jieter I think you might have a better idea than me.
I just ran into the same problem. However, the workaround with adding order_by='name'
does not work for me. The FieldError
shows me only the actual fields of the model in the error message.
The Django admin actually does not allow sorting fields that aren't actual database fields when they are added to list_display
(see docs)
So by default, it should probably disable ordering.
How is sorting accomplished when the data does not come from a QuerySet
?
As a workaround one could do the following:
# views.py
from django.views.generic.base import TemplateView
from django_tables2 import SingleTableMixin
class MyView(SingleTableMixin, TemplateView):
template_name = 'myapp/myview.html'
table_class = MyTable
table_data = list(Model.objects.all())
However, all columns of the table must then be declared along with their verbose name (e.g., with Model._meta.get_field('fieldname').verbose_name
).
I'm wondering if it would make sense to convert the QuerySet
of the current page to a list
to be able to use sort
in the case that the order_by
is not a field of the model?
If I have a model with a
getter
like this:and if I use it in a table without setting the
order-by
field in the Column like this:The page will render fine but if I click on the column header I will get a FieldError: Cannot resolve keyword 'name' into field. This is resolved if I add something like
order_by=('name')
intables.Column
but by default there there is no ordering. Maybe setorderable=False
for getter fields or have some kind of fallback solution?