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

  • Home
  • SEARCH
  • 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 7586397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:25:09+00:00 2026-05-30T19:25:09+00:00

I am using the django default pagination but I am concerned about its efficiency.I

  • 0

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

  • 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-30T19:25:10+00:00Added an answer on May 30, 2026 at 7:25 pm

    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:

    class Word(models.Model):
       the_word = models.CharField(max_length=200)
    

    First, you get all the flagged words that belong to the current user:

    fw = FlagWord.objects.filter(user = user)
    if fw.count():
        if fw.word_set.count():
          wordlist.append(
                       dict(('word':obj.the_word,'usertag':True)
                           for obj in fw.word_set.all())
    

    Finally, filter the word list with the other words that didn’t match. I’ve expanded the code since it contains lots of brackets.

    wordlist.append(dict(
                          ('word':obj.the_word,'usertag':False)
                          for obj in
                             Word.objects.exclude(
                               the_word__in=fw.values_list('word__the_word',flat=True)
                             )
                         )
                   )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using django-haystack for search. By default it is showing oldest objects first
I implemented a simple sitemap class using Django's default sitemap application. As it was
I am having problems using django-tagging . I try to follow the documentation but
I am currently using Django to construct JSON encoded objects which a retrieved using
I'm using django-haystack for a search page on my site. I'm basically done, but
Hi i am using django 1.2 .I my case i have few models which
I want to import the django default settings, using from django.conf import global_settings ,
I'm using Django 1.1, and I have this template, a base template, that all
By default, when a form is created in django using ModelForm, the labels for
I have a Django application that logs users in and out using the standard

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.