Pagination causes duplicate elements in Django

Viewed 59

I've the following ListView:

class SentencesListView(ListView):
    model = Sentence
    paginate_by = 10

    def get_queryset(self):
        return Sentence.objects.all().order_by('-proficiency')

When the user interacts with the webpage, proficiency of all Sentences on the displayed page increases. As a result, when get_queryset will be called again (when user visits the next page), he'll see the same sentences as he did on the previous page.

How can I prevent this?

I've tried the following which didn't work:

class SentencesListView(ListView):
    model = Sentence
    paginate_by = 10

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.cached_query_set = None

    def get_queryset(self):
        if not self.request.GET.get('page') or not self.cached_query_set:
            self.cached_query_set = Sentence.objects.all().order_by('-proficiency')
        return self.cached_query_set

self.cached_query_set is never not None when the test is performed inside get_queryset. Hence, the query set is always computed anew when moving to the next page.

Is there a way to prevent users from experiencing duplicates? Or, is there a way to prevent get_queryset from being called when moving to the next page?

0 Answers
Related