Skip to content

Paginate raw sql queries using LIMIT and OFFSET. It also supports ORDER BY queries with direction

Notifications You must be signed in to change notification settings

bulkan/django-sqlpaginator

Repository files navigation

Build StatusPyPi downloads

django-sqlpaginator

Paginate raw sql queries using LIMIT and OFFSET

It will also supports ORDER BY queries

installation

To install from pypi

pip install django-sqlpaginator 

To get the latest (and possibly non stable version) from git

pip install git+git://github.com/bulkan/django-sqlpaginator.git 

You also need to install sqlparser

pip install git+git://github.com/andialbrecht/sqlparse.git 

In settings.py

INSTALLED_APPS= ( ... 'sqlpaginator', ... )

Thats it !!

usage

Pretty much same as django.core.pagination.Paginator

If you have the following models

classAlbum(models.Model): albumid=models.IntegerField(primary_key=True, db_column=u'AlbumId') title=models.TextField(db_column=u'Title') artistid=models.IntegerField(db_column=u'ArtistId') classArtist(models.Model): artistid=models.IntegerField(primary_key=True, db_column=u'ArtistId') name=models.TextField(db_column=u'Name', blank=True) 

and you want to paginate on Albums, then inside a view;

fromsqlpaginator.paginatorimportSqlPaginatorfrommodelsimportAlbumdefget_albums(request, page=1): sql="select * from %s"%Album._meta.db_tablepaginator=SqlPaginator(sql, Album, page=page, order_by='title') try: albums=paginator.page(page) exceptPageNotAnInteger: # If page is not an integer, deliver first page.albums=paginator.page(1) exceptEmptyPage: # If page is out of range (e.g. 9999), deliver last page of results.albums=paginator.page(paginator.num_pages) returnrender_to_response('albums_list.html',{'albums': albums})

In the template albums_list.html

{%foralbuminalbums%}{# Each "album" is a Album model object. #}{{album.title|upper }}<br/>{%endfor%} <divclass="pagination"><spanclass="step-links">{%ifalbums.has_previous%} <ahref="?page={{albums.previous_page_number }}">previous</a>{%endif%} <spanclass="current">Page{{albums.number }} of{{albums.paginator.num_pages }}. </span>{%ifalbums.has_next%} <ahref="?page={{albums.next_page_number }}">next</a>{%endif%} </span></div>

contributing

  • Clone repo
  • Change code
  • Add tests
  • Run the tests nosetests -s --with-coverage --cover-package=sqlpaginator
  • Submit pull request

About

Paginate raw sql queries using LIMIT and OFFSET. It also supports ORDER BY queries with direction

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •