Skip to content

Instantly share code, notes, and snippets.

@tkrajina
Created April 25, 2014 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkrajina/11298980 to your computer and use it in GitHub Desktop.
Save tkrajina/11298980 to your computer and use it in GitHub Desktop.
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