Skip to content

Instantly share code, notes, and snippets.

Created April 25, 2014 18:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Django paginator with RawQuerySet
# Dummy model:
class DbCounter(models.Model):
count = models.IntegerField()
def get_count(table, where):
# Check strings for sql injections here before doing this!
return DbCounter.objects.raw('select 1 as id, count(*) as count from %s where %s' % (table, where))[0].count
# View:
def index(request):
count = get_count('table', 'suggestion=0')
objects = mod_models.WMS.objects.raw('select * from table where suggestion=0 order by created desc')
objects.count = lambda : count
paginator = Paginator(objects, 5)
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment