https://github.com/bulkan/django-sqlpaginator
Paginate raw sql queries using LIMIT and OFFSET. It also supports ORDER BY queries with direction
https://github.com/bulkan/django-sqlpaginator
Last synced: 8 months ago
JSON representation
Paginate raw sql queries using LIMIT and OFFSET. It also supports ORDER BY queries with direction
- Host: GitHub
- URL: https://github.com/bulkan/django-sqlpaginator
- Owner: bulkan
- Created: 2012-08-20T07:55:47.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2015-07-23T07:08:04.000Z (almost 11 years ago)
- Last Synced: 2025-04-15T21:47:37.369Z (about 1 year ago)
- Language: Python
- Homepage:
- Size: 700 KB
- Stars: 19
- Watchers: 4
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](http://travis-ci.org/bulkan/django-sqlpaginator)
[](https://crate.io/packages/django-sqlpaginator/)
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
```python
INSTALLED_APPS = (
...
'sqlpaginator',
...
)
```
Thats it !!
usage
=====
Pretty much same as django.core.pagination.Paginator
If you have the following models
```python
class Album(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')
class Artist(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;
```python
from sqlpaginator.paginator import SqlPaginator
from models import Album
def get_albums(request, page=1):
sql = "select * from %s" % Album._meta.db_table
paginator = SqlPaginator(sql, Album, page=page, order_by='title')
try:
albums = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
albums = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
albums = paginator.page(paginator.num_pages)
return render_to_response('albums_list.html', {'albums': albums})
```
In the template ```albums_list.html```
```python
{% for album in albums %}
{# Each "album" is a Album model object. #}
{{ album.title|upper }}
{% endfor %}
{% if albums.has_previous %}
previous
{% endif %}
Page {{ albums.number }} of {{ albums.paginator.num_pages }}.
{% if albums.has_next %}
next
{% endif %}
```
contributing
=====
* Clone repo
* Change code
* Add tests
* Run the tests
```nosetests -s --with-coverage --cover-package=sqlpaginator```
* Submit pull request