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 5841791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:56:06+00:00 2026-05-22T11:56:06+00:00

I have 3 models: class Author(models.Model): title = CharField() class Genre(models.Model): title = CharField()

  • 0

I have 3 models:

class Author(models.Model):
    title = CharField()

class Genre(models.Model):
    title = CharField()

class Book(models.Model):
    title = CharField()
    author = ManyToManyField(Author)
    genre = ManyToManyField(Genre)

And I have a checkbox form (multiple choice) with all Genres and Authors, where checkbox value is item (genre or author) id.

Purpose: Show books with selected authors or genres (without duplicates)

I can do this in two ways:

First way:

if request.POST:
    book_list = Book.objects.all() #get all books from db
    books = []  

    request_list = request.POST.getlist('genre') #select list of genres in request
        for item in request_list:
            add_book = report_list.filter(genre=r_request) #queryset of book filtered by each genre 
            books.append(add_book) 
            book_list = book_list.exclude(genre=item)

    request_list = request.POST.getlist('author') #select list of authors in request
        for item in request_list:
            add_book = report_list.filter(author=item) #queryset of book filtered by each author 
            books.append(add_book) 
            book_list = book_list.exclude(author=item)
    return ...
        'books': books

But this way is very slow when I select a lot of authors and genres, becouse excluding is very slow.

Second way:

Remove book_list = book_list.exclude(…) and apply template tag {% ifchanged book.id %}

But I think, this will be very slow too when I’ll get 1000+ books in request result (books)

How can I show books with selected authors and genres fast?

  • 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-22T11:56:06+00:00Added an answer on May 22, 2026 at 11:56 am

    You’re looping through a list, and for each one of the items in the list you’re asking for an SQL query.

    Each SQL query runs over the entire table database to get the results (unless it’s organized properly).

    So, you can use the __in lookup to filter from a list:

    “books that have authors from list and genres from list”

    genre_list= request.POST.getlist('genre')
    author_list = request.POST.getlist('author')
    books = Book.objects.filter(genre__in=genre_list,author__in=author_list)
    

    “books that have the genre or the author”

    from django.db.models import Q
    genre_list= request.POST.getlist('genre')
    author_list = request.POST.getlist('author')
    books = Book.objects.filter(Q(genre__in=genre_list) | Q(author__in=author_list))
    

    You can also read Two or more __in filters in django queryset … They use Q object to concatenate the query into one filter.

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

Sidebar

Related Questions

I Have to tables class Book(models.Model): title = models.CharField(max_length=200) authors = models.ManyToManyField(Individual, related_name=author_for, blank=True,
Short question. I have two models: class Author(models.Model): name = models.CharField(max_length=250) class Book(models.Model): title
Let's say I have some contrived models: class Author(Model): name = CharField() class Book(Model):
I have the following two models class Author(Model): name = CharField() class Publication(Model): title
If I have two models like class Author(models.Model): name = models.CharField(max_length=100) title = models.CharField(max_length=3,
I have a simple Book Author relationship class Author(models.Model): first_name = models.CharField(max_length=125) last_name =
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage

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.