1

I am using django-pagination and django-sorting together for one of my views. Both these have custom middleware to sort and paginate. So is the order of middleware important in this case?

Konstant
  • 2,179
  • 16
  • 32

1 Answers1

1

In this case the order is not important:

  • django-pagination looks for a variable page and
  • django-sorting looks for sort and dir variables.

So both just populate the request object with their variables. If eg some of these required the django.auth framework, order matters: the auth-using framework must come after 'django.contrib.auth.middleware.AuthenticationMiddleware' etc.

Django middleware classes are easily read, so looking at the source, helps a lot :-) understanding what is going on.

jazz
  • 2,371
  • 19
  • 23
  • great thanks. but i am a little confused. so where does the actual sorting happen? we just load the templatetags which generate the required request parameters. We don't change the queryset code in the view, but still it works!!! Where does actual query execution happen in this case? – Konstant Aug 12 '11 at 15:35
  • 1
    Looks like the sorting happens in the template tag. Check out SortedDataNode in the [template tag code](https://github.com/directeur/django-sorting/blob/master/django_sorting/templatetags/sorting_tags.py) – sandinmyjoints Aug 12 '11 at 16:57
  • As William pointed it out, sorting happens at SortedDataNode `context[key] = value.order_by(order_by)` [line 99](https://github.com/directeur/django-sorting/blob/master/django_sorting/templatetags/sorting_tags.py#L99). – jazz Aug 12 '11 at 17:22
  • thanks for the explanation guys. i have one last doubt. by using both sorting and pagination, will i be able to retain the lazy queryset behaviour? Ideally the sorting and pagination should work together like explained in this [answer](http://stackoverflow.com/questions/6653638/django-pagination-overhead-with-sorting). Will this behaviour be retained in this case? How can I check it? – Konstant Aug 16 '11 at 11:01
  • 1
    You could use django_debug_toolbar to test which SQL statements are executed when your Page is rendered. – jazz Aug 16 '11 at 13:29