0

how to set pagination as per page in SQL query, i tried with django default pagination but it didn't worked in my code and i am direct fetching data using SQL raw-query instead of ORM. i think the another way is set pagination using SQL query LIMIT or OFFSET but i have no idea about url-endpoint when i am searching next-page.

class Order_ListAPIView(APIView):

    def get(self,request,format=None):

        if request.method == 'GET':
            cur,conn = connection()
            order_query = ''' SELECT * FROM orders'''
            order_detail_query = ''' SELECT * FROM order_details'''

            with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:

                cursor.execute(order_query)
                order_result = cursor.fetchall()
                order_data = list(order_result)

            ...
             ... #rest_code
              ...


            return Response({"order_data":order_data},status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)
Diado
  • 2,229
  • 3
  • 18
  • 21

1 Answers1

0

I'm not too familiar with Django, but as you say, you're fetching the data raw yourself from the DB with MySQL, so to setup pagination it should be as simple as adding LIMIT $offset, $perpage to your MySQL query...

SELECT * FROM order_details LIMIT 0, 5;

This query, for instance, would get you PAGE 1, with 5 PER PAGE. To get to page 5 with 5 per page, it's LIMIT 4*5, 5 = LIMIT 25, 5. If you wanted to get to page 37 with 10 per page, it would be LIMIT 36*10, 10 = LIMIT 360, 10.

This should solve the pagination issue with the MySQL part. You'll need to setup vars to determine both page and perpage from the user, probably from a GET param.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133