Duplicate elements in Django Paginate after `order_by` call

前端 未结 2 709
暖寄归人
暖寄归人 2021-02-19 11:00

I\'m using Django 1.7.7.

I\'m wondering if anyone has experienced this. This is my query:

events = Event.objects.filter(
    Q(date__gt=my_date) | Q(date         


        
相关标签:
2条回答
  • 2021-02-19 11:35

    Try to use .distinct() on your query before passing it to Paginator.

    0 讨论(0)
  • 2021-02-19 11:36

    oh, shot in the dark, but i think i might know what it is. i wasn't able to reproduce it in sqlite but using mysql. i think mysql trying to sort on a column that has the same value has it returning the same results during slicing

    the pagination splicing basically does an sql statement of SELECT ... FROM ... WHERE (date > D OR date IS NULL) ORDER BY date ASC LIMIT X OFFSET X

    But when date is null I'm not sure how mysql sorts it. So when I tried two sql queries of LIMIT 10 and LIMIT 10 OFFSET 10 it returned sets that had the same rows, while LIMIT 20 produce a unique set.

    you can try to update your order_by to order_by('id', 'date') to have it sort by a unique field first and it may fix it.

    0 讨论(0)
提交回复
热议问题