django-tables2 icon indicating copy to clipboard operation
django-tables2 copied to clipboard

DateField ordering with pagination is not stable

Open Azd325 opened this issue 4 years ago • 2 comments

Hi, We have a paginated table which is ordered by default by a datefield invoice_date.

class InvoiceTable(tables.Table):
    class Meta:
        model = InvoiceTable
        fields = ('invoice_date', 'invoice_number', 'total_net', 'total_gross')
        order_by = "-invoice_date"

Yesterday we had an issue that some invoices are missing in the table, so we started an investigation and find out that the order is not stable because we had over 40 new invoices with the same date which didn't fit on one table page and the next page didn't give us the next following items because the data came back in unspecified way from the database.

We solve it with this one ordering-by-accessors

class InvoiceTable(tables.Table):
    invoice_date = tables.Column(order_by=("invoice_date", "id"))
    class Meta:
        model = InvoiceTable
        fields = ('invoice_date', 'invoice_number', 'total_net', 'total_gross')
        order_by = "-invoice_date"

Maybe we can extend the documentation on the pagination section that in some cases the order is not stable or something else. Not sure if we can add easily the ordering by id at constructing the queryset inside of django-tables2.

Azd325 avatar Feb 20 '20 09:02 Azd325

I have the same issue but with floats that have the same value.

So at the end of Page 1 I have Entry 10 and at the beginning of Page 2 I find Entry 10 again but Entry 11 appears to be missing

Upon further investigation it seems that when multiple entries have the same value pagination doesn't work.

So if my queryset contains

| Entry 1 | 1 | | Entry 2 | 1 | | Entry 3 | 1 | | Entry 4 | 1 |

And there is Entry 1 and Entry 2 displayed on Page 1 expected behaviour would be to see Entry 3 and Entry 4 on Page 2 but it seems to happen, that Entry 1 and Entry 2 can appear instead

13hannes11 avatar Feb 28 '20 17:02 13hannes11

Anything we automagically add to the ordering here might also result in strange cases. So I think a documentation note is the way to go.

@Azd325 Are you able to open a Pull request with such a change?

jieter avatar Mar 31 '20 13:03 jieter