On Wed, Jun 17, 2009 at 8:49 PM, IanR <ianrear...@gmail.com> wrote: > > I've recreated this behavior with the following examples. (using > 1.0) > > #models.py > class Building(models.Model): > name = models.CharField(max_length=10) > floors = models.IntegerField() > > class Unit(models.Model): > unit_number = models.IntegerField() > bedrooms = models.IntegerField() > building = models.ForeignKey(Building) > > #filter_test.py > b1 = Building(name="Bld 1",floors=10) > b1.save() > u = Unit(unit_number=1,bedrooms=1,building=b1) > u.save() > u = Unit(unit_number=2,bedrooms=2,building=b1) > u.save() > u = Unit(unit_number=3,bedrooms=2,building=b1) > u.save() > > > b2 = Building(name="Bld 2",floors=20) > b2.save() > u = Unit(unit_number=1,bedrooms=1,building=b2) > u.save() > u = Unit(unit_number=2,bedrooms=2,building=b2) > u.save() > u = Unit(unit_number=3,bedrooms=1,building=b2) > u.save() > > b3 = Building(name="Bld 3",floors=30) > b3.save() > u = Unit(unit_number=1,bedrooms=1,building=b3) > u.save() > u = Unit(unit_number=2,bedrooms=2,building=b3) > u.save() > u = Unit(unit_number=3,bedrooms=1,building=b3) > u.save() > > buildings = Building.objects.filter(unit__bedrooms=1) > for bld in buildings: > print bld.name > > ######OUTPUT > Bld 1 > Bld 2 > Bld 2 > Bld 3 > Bld 3 > > -------------------------------- > So its returning multiple duplicate Building instances. > > Is there a way to make the query so it only returns 1 Building > instance for each match (rather than the dupes) So ideally it would > have just returned. Bld 1, Bld 2 and Bld 3 because they all happen to > have units with 1 bedroom. It looks like the ORM is returning 1 > Building instance for each Unit that has 1 bedrooms. > > > > > Use the .distinct() function on a queryset.
Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---