#21748: Invalid exclude result for related models
----------------------------------------------+--------------------
     Reporter:  partizan                      |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.6
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 When making exclude query on fields added as reverse relations results are
 incorrect.
 Here is an example:
 models.py
 {{{
 class Author(models.Model):
     name = models.CharField(max_length=120)
     book = models.OneToOneField('Book', null=True, blank=True)

     def __unicode__(self):
         return self.name

 class Reader(models.Model):
     name = models.CharField(max_length=120)
     book = models.OneToOneField('Book', null=True, blank=True)

     def __unicode__(self):
         return self.name

 class Book(models.Model):
     name = models.CharField(max_length=120)

     def __unicode__(self):
         return self.name

 class SimpleBook(models.Model):
     name = models.CharField(max_length=120)
     author = models.CharField(max_length=120, null=True, blank=True)
     reader = models.CharField(max_length=120, null=True, blank=True)

     def __unicode__(self):
         return "%s" % self.name
 }}}

 Test data:
 {{{
 Book                    | Author        | Reader
 The Lord of the Rings   | JRR Tolkien   | John
 Pride and Prejudice     | Jane Austen   | x
 His Dark Materials      | x             | x
 }}}

 Correct query results (django 1.5):

 One book without author and reader, two books, excluding books without
 author and reader.
 {{{
 In [2]: Book.objects.exclude(author=None, reader=None)
 Out[2]: [<Book: The Lord of the Rings>, <Book: Pride and Prejudice>]

 In [3]: Book.objects.filter(author=None, reader=None)
 Out[3]: [<Book: His Dark Materials>]
 }}}

 Incorrect query results (django 1.6):
 One book without without author and reader. If excluded - only one book is
 selected instead of two.
 {{{
 In [2]: Book.objects.exclude(author=None, reader=None)
 Out[2]: [<Book: The Lord of the Rings>]

 In [3]: Book.objects.filter(author=None, reader=None)
 Out[3]: [<Book: His Dark Materials>]
 }}}

 Expected results: When exluding books without author and reader query must
 return 2 books. Just like it does with simple fields(which is not reverse
 relations), django 1.6:

 {{{
 In [2]: SimpleBook.objects.exclude(author=None, reader=None)
 Out[2]: [<SimpleBook: The Lord of the Rings>, <SimpleBook: Pride and
 Prejudice>]

 In [3]: SimpleBook.objects.filter(author=None, reader=None)
 Out[3]: [<SimpleBook: His Dark Materials>]
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21748>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.e0e367e524f64b0b63729c342d730ce1%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to