django query, order results such that th128 > th99

18 views
Skip to first unread message

gintare

unread,
Sep 28, 2015, 2:42:19 AM9/28/15
to Django users
Is it possible to order the query result strings in such a way that th128 string would be first comparing to th99.
The query results contains 128 items in format: "th"+number. For instance, th1, th2, ...th80, ..th99, th100, th128.
Than i order results, they go in the following order, th1, th10,th100, th101,th102,... th11,th110, ... th2, th20, th21,th22,.. th9,..th99.
Thus i am not able to get the largest result, which is th128. I am getting th99 instead.

def temp(request):
    query = request.GET.get( 'q','')
    #a=Aref5.objects.filter(Rname__contains='book').count()  #finds128
    a=Aref5.objects.filter(Rname__contains='book').order_by('-Rname')[0:1].get() #gets aref5 object
    print(a.Rname) #prints th99 instead of th128, which logically is larger
    return render_to_response('Ath.html', {"query": query})

Gergely Polonkai

unread,
Sep 28, 2015, 2:58:30 AM9/28/15
to Django users

Hello,

what you want to achieve is called "natural order sorting" which is not built into any common SQL implementation I know. You have to do it manually from code, or, if you have to do pagination, add a sorting column that is numeric (and indexed).

Best,
Gergely

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/69e26bab-210e-4137-adf3-498855d0eae6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages