If you are getting the correct resultset, just with parents multiple times, add .distinct() to your queryset.
On Apr 24, 1:17 am, Kevin L <[EMAIL PROTECTED]> wrote: > Hello Django users, I'm having trouble with an exclude statement and > was hoping some kind soul could help me out. My models are simple, > it's basically just two models with a ForeignKey relationship. > > class Parent(models.Model): > pass > class Child(models.Model): > name = models.CharField(maxlength=100) > parent = models.ForeignKey(Parent) > > I want to get every parent who does not have a child named "Bob". I > initially thought this would just be one exclude statement - > > Parent.objects.exclude(child__name="Bob")[:100] > > However, this gives me a list that includes many parents multiple > times - basically each parent is in the list once for each child not > named Bob, even if they do have a child named Bob. This makes sense > looking at the sql query - > > SELECT "foo_parent"."id" FROM "foo_parent" INNER JOIN "foo_child" AS > "foo_parent__child" ON "foo_parent"."id" = > "foo_parent__child"."parent_id" WHERE ((NOT > ("foo_parent__child"."name" = Bob))) DESC LIMIT 100 > > But what I really want is to discard a parent entirely if they have a > child named Bob. > > Any advice for what I'm doing wrong here, or suggestions on a > workaround? I'm using django version 0.96. Often > 99% of the parents > have children named Bob, so I'd rather avoid filtering them out in > python. > > Thanks in advance! --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---