Django paginator with RawQuerySet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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