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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:13:04+00:00 2026-06-11T08:13:04+00:00

Models : class Store(models.Model): name = models.CharField(max_length=100) greatStore = models.BooleanField() class Book(models.Model): stores =

  • 0

Models :

class Store(models.Model):
   name = models.CharField(max_length=100)
   greatStore = models.BooleanField()

class Book(models.Model):
   stores = models.ManyToManyField(Store)
   name = models.CharField(max_length=200,unique=True)
   fantasticBook = models.BooleanField()   

I want get the list of all the books which are in great stores. Reading the documentation, it says : https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships that I should write :

myBooks =Book.objects.filter(stores__greatStore=True)

but this merely does a JOIN with a Store.greatStore==TRUE which is NOT what I want !

For instance if I have 2 stores (both are great stores) and one book in my database which belongs to the 2 stores, myBooks is going to have 2 elements : twice the same book.

I wanted the collection of books which have at least one great store, not the first element of couples (b,s) which verifies (s belongs to b.stores and s.greatStores==true)

EDIT :

Why I think Django’s API is misleading :
Let’s keep the same example : 2 great stores and book (fantasticBook==true) which belongs to both store.
If you run the request :

myBooks = Book.objects.all()

you get only one result : the only book that exists.
If you run the request :

myBooks = Book.objects.filter(coolBook=True)

you get only one result : the only book that exists.
If you run the request :

myBooks = Book.objects.filter(stores__greatStore=True)

It seems to mean that you get the collection of books which have at least one great store but this is not what we get because myBooks has 2 results.

  • 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-06-11T08:13:05+00:00Added an answer on June 11, 2026 at 8:13 am

    You can use distinct() which will give you unique results for Book like:

    myBooks =Book.objects.filter(stores__greatStore=True).distinct()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Author(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() friends = models.ManyToManyField('self', blank=True) class Publisher(models.Model):
class Book(models.Model): author = models.ForeignKey(User) name = models.CharField(max_length=100) def view(request): book = Book.objects.get(pk=1) request.session['selected_book']
class Person(models.Model): name = models.CharField(max_length=100) class Entry(models.Model): text = models.CharField(max_length=100) person = models.ManyToManyField(Person, blank=True,
In this scenario, I have 2 or more models: class Store(models.Model): name = models.CharField(max_length
model: class Store(models.Model): name = models.CharField(max_length = 20) class Admin: pass def __unicode__(self): return
I have two models like this: class Store(models.Model): name = models.CharField(max_length=255) class Order(models.Model): store
I have these models: class App(models.Model): name = models.CharField(max_length=100) class ProjectA(models.Model): name = models.CharField(max_length=100)
In my model I have: class Poll(models.Model): topic = models.CharField(max_length=200) tags = models.ManyToManyField(Tag) I'm
I have 3 model classes: class Team(models.Model): name = models.CharField(max_length=100, default=, blank=True, null=True) number
My Meeting model is Here class Meeting(models.Model): name = models.CharField(max_length=200) meetingID = models.CharField(max_length =

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.