UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'myapp.models.Movies'> QuerySet.this is the code snippets

when i try to add pagination in my app above error message shows up and no pagination is working.

this is my view:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Movies
from django.core.paginator import Paginator

def home(request):
paginator= Paginator(movie, 3)

return render(request,'myapp/home_page.html',{'movies':movie})

this is my home.html
{% extends “myapp/base.html” %}
{% block content %}

.inner { overflow:hidden; } .inner img { transition: all 1.5s ease; } .inner:hover img{ transform: scale(1.5); }
{% for movie in movies %}
            <div class="card-body">
                <h5><a class="card-title">{{ movie.title }} </a></h5>
                <p class="card-text text-success "> Genre//{{ movie.genre }}</p>
                <p class="card-text text-danger "> Price:${{ movie.price }}</p>
                <p class="card-text text-info "> Rating: {{ movie.rating }}</p>
                <a href="#" class="btn btn-primary">details</a>
    {% endfor %}
    {% if movie.has_previous %}
    <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
    <a class="btn btn-outline-info mb-4" href="?page{{ movie.previous_page_number}}">Privious</a>
    {% endif %}
    {% for num in movie.paginator.page_range %}
        {% if movie.number == num %}
            <a class="btn btn-info mb-4" href="?page{{ num }}">{{ num }}</a>
        {% elif num > movie.number|add:'-1' and  num < movie.number|add:'1' %}
          <a class="btn btn-info mb-4" href="?page{{ num }}">{{ num }}</a>
        {% endif %}
    {% endfor %}
    {% if movie.has_next %}
    <a class="btn btn-outline-info mb-4" href="?page={{movie.next_page_number }}">Next</a>
    <a class="btn btn-outline-info mb-4" href="?page{{ movie.paginator.num_pages }}">Last</a>
    {% endif %}

{% endblock content%}

1 Like

A database, by default, provides an unordered and theoretically indeterminate sequence of results. You can issue two of the same query and get a different order and they both be valid.
When you’re trying to do server-side pagination, that means that you might get some of the same rows on page 2 as you had seen on page 1. Those are the inconsistent results being referred to in the warning message.

The resolution to this is to ensure that some ordering is applied to your queryset. Either adding an order_by clause on the queryset or an ordering in the Meta for that model.



Thank you sir, i have figure it out.

Please do not cross-post on topics. When you have a question, start a new topic.

1 Like