On Thu, Oct 20, 2011 at 12:40 PM, Tim Chase <django.us...@tim.thechases.com> wrote: > On 10/19/11 20:35, Tim Chase wrote: > > Perhaps I should have changed the test to be more clear, instead of using > >> results = m.Person.objects.filter( >> Q(name__iexact=alias) | >> Q(aliases__alias__iexact=alias) >> ) >> self.assertEqual(1, results.count(), >> "Failed #%i on %r" % (i, alias),) > > use > > self.assertEqual( > m.Person.objects.count(), #only 1 > results.count(), # comes back as 6 for "William" > "Failed #%i on %r" % (i, alias), > ) > > This makes it clearer that there's only the one person in the DB (no > fixtures being loaded). The underlying SQL brings back the number of rows > caused by the join of Person with Alias, but it seems a bug that > .objects.filter().count() should ever return more than .objects.count() > > -tkc >
It might seem that way, but it is definitely not a bug. count() counts the number of rows found by the query, and your query can find the same row multiple times, as you are joining across a M2M relationship. If you want distinct results from the query, make sure that you tell django so by using distinct(). Eg: >>> Type.objects.filter(department__backend_type__name='Other') [<Type: Manufacturer>, <Type: Manufacturer>, <Type: Manufacturer>, <Type: Manufacturer>, <Type: Manufacturer>] >>> Type.objects.filter(department__backend_type__name='Other').count() 5 >>> Type.objects.filter(department__backend_type__name='Other').distinct() [<Type: Manufacturer>] >>> Type.objects.filter(department__backend_type__name='Other').distinct().count() 1 Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.