Skip to content

Instantly share code, notes, and snippets.

Embed
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
You can’t perform that action at this time.