id,summary,reporter,owner,description,type,status,component,version,severity,resolution,keywords,cc,stage,has_patch,needs_docs,needs_tests,needs_better_patch,easy,ui_ux 21748,Invalid exclude result for related models,Sergey Tereschenko,nobody,"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]: [, ] In [3]: Book.objects.filter(author=None, reader=None) Out[3]: [] }}} 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]: [] In [3]: Book.objects.filter(author=None, reader=None) Out[3]: [] }}} 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]: [, ] In [3]: SimpleBook.objects.filter(author=None, reader=None) Out[3]: [] }}} ",Bug,closed,"Database layer (models, ORM)",1.6,Normal,fixed,,,Ready for checkin,1,0,0,0,0,0