[Fixed]-Django+python: Use Order by to Django RawQuery objects?

0👍

Below is my try & I got the desired answer.

I fetch the data from database & is stored in the form of array of dictionary in a list.

Assuming the data stored format in a list:

l = [{'productName'='soap',id=1},{'productName'='Laptop',id=2}]

Below code snippet is the solution to sort dependening upon key:

from operator import itemgetter

for Ascending

res= sorted(l, key=itemgetter('name'))

For Descending

res= sorted(l, key=itemgetter('name'),reverse=True)
👤Sandy

1👍

I think what you are looking for is pagination. This is an essential technique when you want to display data in batches(pages).

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def listing(request):
    query_string = 'your query'
    query_args = []
    conn = connections['databaseName']
    with conn.cursor() as cursor:
    cursor.execute(query_string, *query_args)
    all_records = dictfetchall(cursor)
    paginator = Paginator(all_records, 100) # Show 100 records per page

    page = request.GET.get('page')
    try:
        records = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        records = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        records = paginator.page(paginator.num_pages)

    return records

Whenever you make a request you should include the page you want to display (e.g. 1,2,3…) in the url parameters.

Example GET http://localhost/products/?page=1

In terms of logic, your javascript should display the first page and have a counter that hold the next page you should request, after the user scrolls make an AJAX request to get the second page and increase the page counter, etc…

EDIT: As for the sorting matter you can also use javascript to sort your data

Leave a comment