Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable ordering by default for django_tables2 Tables

I use django_tables2 to render my tables. For one table, I only want to see the most recent 5 entries. Therefore, I need to order my queryset before passing it to the table object:

qset = myObject.objects.order_by('-start_time').all()[:5]
table = MyTableClass(qset, template='path/to/template.html')

This yields the following error message:

AssertionError: Cannot reorder a query once a slice has been taken.

I could set orderable=False for every django_tables.Column, but since MyTableClass inherits from another table class, I would like to avoid this.

Thanks in advance

like image 709
ezdazuzena Avatar asked Nov 17 '17 14:11

ezdazuzena


1 Answers

From: http://django-tables2.readthedocs.io/en/latest/pages/api-reference.html#table-meta

orderable (bool): Default value for column’s orderable attribute.

If the table and column don’t specify a value, a column’s orderable value will fallback to this. This provides an easy mechanism to disable ordering on an entire table, without adding orderable=False to each column in a table.

So this solves my Problem:

class MyTableClass(django_tables2.Table):
    class Meta:
        orderable = False
    ...

Update: As mentioned by @Jieter in the comments, passing it as an argument should also work (didn't check that):

table = MyTable(data, orderable=False)
like image 161
ezdazuzena Avatar answered Sep 30 '22 17:09

ezdazuzena