Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:00:24+00:00 2026-05-28T00:00:24+00:00

def mysearch(request): This view builds a Q object query based on which fields are

  • 0
def mysearch(request):
    """This view builds a Q object query based on which fields are filled."""
    if 'submit' in request.POST:
        # build Q object depending on fields submitted
        q = Q()
        if request.POST['first_field']:
            q &= Q(firstfield__icontains = request.POST['first_field'])

        ...

        if request.POST['sixth_field']:
            q &= Q(sixthfield__icontains = request.POST['sixth_field'])

        results_list = MyModel.objects.filter(q)
        count = len(results_list)

        # store results
        request.session['results_list'] = results_list
        request.session['count'] = count

    # 'p' is an arbitrary marker to detonate pagination of a page other than 1 
    if 'p' in request.GET:
        results_list = request.session['results_list']
        count = request.session['count']

    if count and count > 0:
        ...
        # pagination code
        ...

    else:
        pass
    return render_to_response('search_results.html',
        locals(), context_instance=RequestContext(request)) 

All works well in my templates using the paginator. The problem is that Django debug toolbar tells me that I am hitting the database the same number of times on pages > 1 than I am on the first page. Why is this? In fact – why is it hitting the database at all? Shouldn’t the whole results_list be being pulled from request.session? Any advice much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:00:25+00:00Added an answer on May 28, 2026 at 12:00 am

    You’re saving a queryset object in the session. Querysets are like SQL statements, but they cache the results. You haven’t run the query when you put it in the session, so what you’re storing is essentially just the query. When it gets pulled out it’s still just a query that hasn’t run, so the queryset gets run again. To ensure that you’re storing just the actual results, do this:

    request.session['results_list'] = list(results_list)
    

    and to save you another query when you find count you can…

    request.session['count'] = len(request.session['results_list'])
    

    Also keep in mind that session data is (by default) saved in the database, so you might not be doing yourself any favors by storing a python pickled representation of the full data. It might in fact be faster just to go to the original table and pull it out that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def myFunc(arg1, arg2): print This is a test with + arg1 + and +
def display_home(request): from datetime import * now=datetime.today() print 'Month is %s'%now.month events=Event.objects.filter(e_date__year=datetime.today().year).filter(e_date__month=datetime.today().month,e_status=1).values('e_name','e_date') return render_to_response("SecureVirtualElection/home.html",{'events':
def register(request): flag = True possible = '0123456789abcdefghijklmnopqrstuvwxyz' token = '' current_datetime = datetime.datetime.now()
def confirmemail(request,token): user = Users.objects.get(email_token = token) if user: user.email_token = '' user.email_token_expiry =
def register(request): flag = True possible = '0123456789abcdefghijklmnopqrstuvwxyz' token = '' length = 10
I have a Rails application and I'm using jQuery to query my search view
def function(message): if 'START' in message: timer = 10 #start a thread which sleeps
def isBig(x): if x > 4: return 'apple' else: return 'orange' This works: if
def double(a) a*2 end method_object = method(:double) and here's my question, how does this
def register(request): flag = True possible = '0123456789abcdefghijklmnopqrstuvwxyz' token = '' length = 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.