I am using the django default pagination but I am concerned about its efficiency.I have 3000+ objects stored in my database and I display 15 each page.So every time I go to the next page it seems to me I have do database query again in my views.My code in views.py is just like the code given in the docs of Django.Here is my code:
all_words=Word.objects.all()
user=request.user
wordlist = []
for word in all_words:
taged_word = FlagWord.objects.filter(word = word,user = user)
if taged_word :
usertag = True
else:
usertag = False
wordlist.append({'word':word,'usertag':usertag})
number = Word.objects.count()
paginator = Paginator(wordlist,15)
try:
page = int(request.GET.get('page','1'))
except ValueError:
page = 1
try:
listpage = paginator.page(page)
except (EmptyPage,InvaildPage):
listpage = paginator.page(paginator.num_pages)
return render_to_response('GRETemplate/wordbank.html',{'words':listpage,'user':user,'number':number})
It’s like I have my wordlist ready for the first time but I have to do it over and over again when i request for another page.Is there any more efficient way to do this?
Thanks.G
You should ideally cache the results of
Word.objects.all()so the system doesn’t hit your db everytime.However, I think your loop could be simplified a little:
Warning: I haven’t tested it.
Suppose your Word model is:
First, you get all the flagged words that belong to the current user:
Finally, filter the word list with the other words that didn’t match. I’ve expanded the code since it contains lots of brackets.