I'm currently building a simple search method for some designs. The query searches both the name of the author and the text of the design. The problem: what happens when the database has 300,000 designs and I want to paginate the results? If I pass a page
variable, then each time a user switches to a different page, the query is executed again.
What is the best way to solve this problem? Is it properly caching as many searches as possible? Is it storing a certain amount in session data?
"""
Searches by screenname or design text
"""
def search_designs(request):
designs = None
words = None
if request.method == 'POST':
q = request.POST['search']
words = q.split()
# Get all approved designs
designs = Design.objects.filter(status=2)
for w in words:
designs = designs.filter(name__icontains=w) | designs.filter(author__profile__screenname__icontains=w)
vars = RequestContext(request,
{
'results' : designs,
'words' : words,
})
return render_to_response("search_test.html", vars)