Solved: Pagination may yield inconsistent results with an unordered object_list

When we added pagination with Django REST Framework “How to implement Pagination with Django & Django REST Framework” and started the server, we got a warning as below,

/workspace_class_based_views/env/lib/python3.6/site-packages/rest_framework/pagination.py:200: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'helloproject.helloapp.models.UserInfo'> QuerySet.
  paginator = self.django_paginator_class(queryset, page_size)
[DD/Sep/YYYY HH:MM:SS] "GET /users/ HTTP/1.1" 200 12671

Solution :

set the proper ordering of the json objects based on the fields in your JSON by modifying models.py.

For example, in our code, we changed UserInfo model to make sure the results are ordered based on primary key by changing the code as,

$ vim helloproject/helloapp/models.py

class UserInfo (models.Model) :
    userid = models.CharField(max_length=100)
    username = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    age = models.CharField(max_length=100)

    #useraddress = models.ForeignKey('UserAddress', on_delete=models.CASCADE)
    useraddress = models.ManyToManyField('UserAddress')

    class Meta:
        ordering = ['id']

    def __str__(self) :
        return self.username

So, when we see the result JSON, you can see that the order of JSON objects will be based on “id” i.e. 1, 2, 3 etc

    "results": [
        {
            "id": 1,
            "userid": "my_userid",
            "username": "lynxbee1",
            "email": "social(at)lynxbee.com",
            "age": "45",
            "useraddress": [
                {
                    "id": 6,
                    "home_no": "123",
                    "street": "my home street",
                    "city": "my city",
                    "pincode": 123456
                }
            ]
        },
        {
            "id": 2,
            "userid": "my_new_userid",
            "username": "lynxbee1",
            "email": "social(at)lynxbee.com",
            "age": "45",
            "useraddress": [
                {
                    "id": 7,
                    "home_no": "123",
                    "street": "my home street",
                    "city": "my city",
                    "pincode": 123456
                },
                {
                    "id": 8,
                    "home_no": "01",
                    "street": "my 01 home at street",
                    "city": "my current city",
                    "pincode": 999999
                }
            ]
        }
    ]

Now, lets say we want our ordering to be based on “userid” , then we need to change the code to use ordering as,

    class Meta:
        ordering = ['userid']

In this case, we can see that JSON objects will be ordered based on the alphabets in “userid” and the same above listed JSON gets rearranged as,

"results": [
        {
            "id": 2,
            "userid": "my_new_userid",
            "username": "lynxbee1",
            "email": "social(at)lynxbee.com",
            "age": "45",
            "useraddress": [
                {
                    "id": 7,
                    "home_no": "123",
                    "street": "my home street",
                    "city": "my city",
                    "pincode": 123456
                },
                {
                    "id": 8,
                    "home_no": "01",
                    "street": "my 01 home at street",
                    "city": "my current city",
                    "pincode": 999999
                }
            ]
        },
        {
            "id": 1,
            "userid": "my_userid",
            "username": "lynxbee1",
            "email": "social(at)lynxbee.com",
            "age": "45",
            "useraddress": [
                {
                    "id": 6,
                    "home_no": "123",
                    "street": "my home street",
                    "city": "my city",
                    "pincode": 123456
                }
            ]
        }
    ]

1 thought on “Solved: Pagination may yield inconsistent results with an unordered object_list”

  1. hello my question is this error UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: QuerySet.
    paginator = Paginator(posts, 3)

    Reply

Leave a Comment