Search code examples
djangodjango-tables2

Sorting tables when multiple tables in a view (django-tables2)


Is it possible to sort two (or more) tables independently in a single view?

I have two tables in a view: one through CBV SingleTableView (Table A) and one passed in through the context (Table B). When trying to sort Table B, the correct url is passed e.g. https//:..../?sort=delivery_date however it seems it is being consumed by Table A. If the column name does not exist in Table A, nothing happens, if it does exist in Table A, Table A is sorted rather than Table A.

I can understand this behaviour as nothing is posted to identify the originating table. My question is, is there any support for independent multiple table per view sorting?

Thanks in advance Nathan


Solution

  • Well that was easy...

    It was in the docs of course: I needed to add a prefix to my second table:

    ...
    config = RequestConfig(self.request)
    table2 = ot.UnfulfilledSalesOrderTable(om.SalesOrder.objects.filter(
        status__fulfilled=False, status__cancelled=False), self.request,
        prefix="2-")
    config.configure(table2)
    ...
    

    This adds a prefix to the querystring like so ?sort=ship_date&2-sort=number which achieves exactly the functionality I am after.